0% found this document useful (0 votes)
39 views18 pages

FOP Lab Report 4 - Merged

The document summarizes a lab report on strings, functions, and classes. The objectives of the lab work were to introduce strings, functions like getline and cin.ignore, and classes/objects including constructors. The theory section covered strings, getline/cin.ignore functions, classes/objects, and default/parameterized constructors. The lab work section described 6 programs - the first 3 covered strings, getline, and cin.ignore, the 4th made a basic class, and the last 2 demonstrated default and parameterized constructors. The lab task was to make a program comparing student and father marks using various class functions. Two methods were described - one using functions taught in class, and another making a new project class

Uploaded by

Ali Arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views18 pages

FOP Lab Report 4 - Merged

The document summarizes a lab report on strings, functions, and classes. The objectives of the lab work were to introduce strings, functions like getline and cin.ignore, and classes/objects including constructors. The theory section covered strings, getline/cin.ignore functions, classes/objects, and default/parameterized constructors. The lab work section described 6 programs - the first 3 covered strings, getline, and cin.ignore, the 4th made a basic class, and the last 2 demonstrated default and parameterized constructors. The lab task was to make a program comparing student and father marks using various class functions. Two methods were described - one using functions taught in class, and another making a new project class

Uploaded by

Ali Arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Fundamentals of Programming

Lab Report 04

Strings, Functions And Classes


SUMMER SEMESTER

Submitted to: Engr. Ali Hassan


Session: ME-14 Section:B
SUBMITTED BY
Name CMS Objective Theory Lab Lab
(1) Work(3) Total
(3) Task (3)
Muhammad Ali 406356
Arshad

School of Mechanical and Manufacturing Engineering


OBJECTIVES
The objectives of today’s lab work include

 Introduction to strings
 Use of getline and cin.ignore
 Introduction to functions
 Introduction to class and objects
 Use of Constructors

THEORY

1. Strings
String is one of the most useful data types in C++. It is basically a collection of characters. The string
data type is used to store a sequence of letters or other characters. To store it, we need to declare
‘string’:-

string Ahmed,Ali;

2. Getline and cin.ignore Functions


The getline() function in C++ reads a line of text from input, including spaces, and stores it as a
string variable. It's useful for capturing entire lines from the user or files.

The cin.ignore() function is used to clear or ignore a certain number of characters from the input
buffer, often after reading input using cin. This helps prevent unwanted characters from affecting
subsequent input operations.

Together, these functions assist in managing input effectively, ensuring that the program handles
user inputs and data files smoothly, without unexpected issues.

3. Classes and Objects


Classes serve as templates, encapsulating both data (attributes) and functions (methods) that
define a certain type of object. Objects are instances of these classes, representing real entities or
concepts. They hold data unique to their instance and can perform actions using the methods
defined in the class. This approach promotes modularity and reusability in code, allowing you to
create and manage multiple instances of the same structure with distinct properties. Classes and
objects collectively enable object-oriented programming, enhancing code organization and
maintainability.

4. Default and Parametrized Constructor


A default constructor is automatically generated by the compiler if no constructor is defined in a
class. It initializes class members with default values, providing a way to create objects without
specifying initial values explicitly.

A parameterized constructor, on the other hand, is defined with parameters in a class. It allows you
to initialize object members with specific values provided as arguments when the object is created.
This constructor provides greater flexibility, enabling objects to be created with customized initial
states.

LAB WORK

Program#1
We made a program using string data type for taking entries of name from user and printing his/her
full name.

Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name_1;
string name_2;
cout<<"Enter your first name: ";
cin>>name_1;
cout<<"\nEnter your second name: ";
cin>>name_2;
cout<<"Your full name is: "<<name_1<<" "<<name_2;
return 0;
}
Output;

Program#2
We made a program using getline function which could take spaced entries in string.

Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string son_name;
string father_name;
cout<<"Please enter your full name : ";
getline(cin,son_name);
cout<<"Please enter your father's full name : ";
getline(cin,father_name);
cout<<"Your full name is "<<son_name<<endl;
cout<<"Your father's full name is "<<father_name<<endl;
return 0;
}

Output:

Program#3
We made a program making use of cin.ignore() to print only initials of name entered by user.
Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
char f, l;
cout<<"Please enter your full name: ";
first=cin.get();

last=cin.get();
cout<<"Your initials are: "<<f<<l;
return 0;
}

Output:

Program#4:
We made a basic program made a class in it and made basic addition and subtraction functions
using an object.

Code:
#include <iostream>
using namespace std;
class summer
{
private:
int a, b;
public:
int sum();
int diff();
};
summer::sum()
{
cout << "\nEnter the first number: ";
cin >> a;
cout << "\nEnter the second number: ";
cin >> b;
cout<<"Sum of the numbers is "<<a + b;
}
summer::diff()
{
cout << "Enter the first number: ";
cin >> a;
cout << "\nEnter the second number: ";
cin >> b;
cout<<"Difference of the numbers is "<<a - b;
}
int main()
{
summer solution;
solution.sum();
cout<<endl;
solution.diff();

return 0;
}

Output:

Program#5
We made a program to demonstrate the use of default constructor alongside classes.
Code:
#include<iostream>
using namespace std;
class Def
{
private:
int a,b;
public:
Def()
{
a=10;
b=20;
}
output()
{
cout<<a<<endl;
cout<<b<<endl;
}
};
int main()
{
Def aa;
aa.output();
return 0;
}

Output:

Program#6:
We made a program which made use of parametrized constructor in a class.

Code:
#include<iostream>
using namespace std;
class def
{
private:
int a,b;
public:
def(int m,int n)
{
a=m;
b=n;
}
answer();
};
def::answer()
{
cout<<"\nValue of a: "<<a;
cout<<"\nValue of b: "<<b;
}
int main()
{
int m,n;
cout<<"Put values of m and n ";
cin>>m>>n;
def aa(m,n);
aa.answer();
return 0;
}

Output:
LAB TASK

Task:
We had to make a program using a class which had the following functions:

a) A function to enter full name then marks in any subject and again fathers name and then marks
of father in the same subject XD. It should then compare marks and decide the name of the winner
and print it. We had to use getline,cin.ignore,cin.get()

b) Using a default constructor that would initialize the private variables with your name and marks
plus name of your father and marks in the same subject.

c) Using a parameterized constructor for all the above mentioned 4 entries .

We had to make this program using 2 methods one of which was used in the class taught by sir and
for the other one we had to make a project.

Method#1 : Used In Class Taught By Sir :

Code:
#include<iostream>
#include<string>
using namespace std;
class comp
{
private:
int markss_1,marksf_1;
string sonname_1,fathername_1,subject_1;
int markss_2,marksf_2;
string sonname_2,fathername_2,subject_2;

string son,father;
int s,f;
public:
comp () //b) default constructor used to initialize private variables for name and marks of student
and father.
{
markss_2=73;
marksf_2=89;
sonname_2 = "Muhammad Ali";
fathername_2 ="Arshad Ahsan";
subject_2= "Calculus";
}
comp (int m,int n,string h ,string j) //c) paramterized constructor for entries of marks and names.
{
s=m;
f=n;
son=h;
father=j;
}
input();
output();
output_2d();
output_4();

};
comp :: input() // a) Simple Function taking entries for marks in subject from user and comparing
them to print output.
{
cout<<"\n\nStudent Father Set 1 :\n";
cout<<"\nPLease enter the name of the student: ";
getline(cin,sonname_1);
cout<<"\nPLease enter the name of the student's father : ";
getline(cin,fathername_1);
cout<<"\nPLease enter the name of subject : ";
getline(cin,subject_1);
cout<<"\nPlease enter the student's marks in "<<subject_1<<" : ";
cin>>markss_1;
cout<<"\nPlease enter the father's marks in "<<subject_1<<" : ";
cin>>marksf_1;
}
int comp :: output()
{
if (markss_1>marksf_1)
{
cout<<"\nThe student ,"<<sonname_1<<", has more marks than his father ,"<<fathername_1<<", in
"<<subject_1<<"\n\nThe student ,"<<sonname_1<<", is the winner" ;
}
else if (markss_1==marksf_1)
{
cout<<"\nYou ,"<<sonname_1<<", and your father "<<fathername_1<<" have equal marks in
"<<subject_1<<"\n\nIt is a tie ." ;
}
else
{
cout<<"\nThe father, "<<fathername_1<<" ,has more marks than his son ,"<<sonname_1<<", in
"<<subject_1<<"\n\nThe father ,"<<fathername_1<<", is the winner" ;
}
int comp :: output_2d() //b)Function using default constructor
{
cout<<"\n\nStudent Father Set 2 (Initialized Using default constructor) : \n\n";
}
if (markss_2>marksf_2)
{
cout<<"\n\nYou ,"<<sonname_2<<", have more marks than your father ,"<<fathername_2<<", in
"<<subject_2<<"\\nnYou ,"<<sonname_2<<", are the winner" ;
}
else if (markss_2==marksf_2)
{
cout<<"\n\nYou ,"<<sonname_2<<", and your father "<<fathername_2<<" have equal marks in
"<<subject_2<<"\n\nIt is a tie .";
}
else
{
cout<<"\n\nYour father, "<<fathername_2<<" ,has more marks than you ,"<<sonname_2<<", in
"<<subject_2<<"\n\nYour father ,"<<fathername_2<<", is the winner" ;
}
comp :: output_4() //c) Function using paramterized constructor
{

if (s>f)
{
cout<<"\n\nYou ,"<<son<<", have more marks than your father ,"<<father<<", in FSc. ";
}
else if (s==f)
{
cout<<"\n\nYou ,"<<son<<", and your father "<<father<<" have equal marks in FSc. ";
}
else
{
cout<<"\n\nYour father, "<<father<<" ,has more marks than you ,"<<son<<", in FSc. ";
}
int main()
{
int m, n;
string h, j;
comp cc;
cc.input();
cc.output();
cc.output_2d();
cout<<"\n\nStudent Father Set 3 :\n";
cout << "\nPLease enter your name : ";
cin.get();
getline(cin, h);
cout << "\nPLease enter your father's name : ";
getline(cin, j);
cout << "\nPlease enter your marks in FSc : ";
cin >> m;
cout << "\nPlease enter your father's marks in FSc : ";
cin >> n;
comp dd(m, n, h, j);
dd.output_4();
return 0;
}
Output:

Method # 2: By making a project:

Class File (comp.h)

Code:
#ifndef COMP_H
#define COMP_H
#include <string>
class comp {
private:
int markss_1, marksf_1;
std::string sonname_1, fathername_1, subject_1;
int markss_2, marksf_2;
std::string sonname_2, fathername_2, subject_2;
std::string son, father;
int s, f;
public:
comp();
comp(int m, int n, std::string h, std::string j);
void input();
void output();
void output_2d();
void output_4();
};
#endif

Function Definition File(comp.cpp)

Code:
#include "comp.h"
#include <iostream>
using namespace std;
comp::comp() {
markss_2 = 73;
marksf_2 = 89;
sonname_2 = "Muhammad Ali";
fathername_2 = "Arshad Ahsan";
subject_2 = "Calculus";
}
comp::comp(int m, int n, string h, string j) {
s = m;
f = n;
son = h;
father = j;
}
void comp::input() {
cout<<"\n\nStudent Father Set 1 :\n";
cout<<"\nPLease enter the name of the student: ";
getline(cin,sonname_1);
cout<<"\nPLease enter the name of the student's father : ";
getline(cin,fathername_1);
cout<<"\nPLease enter the name of subject : ";
getline(cin,subject_1);
cout<<"\nPlease enter the student's marks in "<<subject_1<<" : ";
cin>>markss_1;
cout<<"\nPlease enter the father's marks in "<<subject_1<<" : ";
cin>>marksf_1;
}
void comp::output() {
if (markss_1>marksf_1)
{
cout<<"\nThe student ,"<<sonname_1<<", has more marks than his father
,"<<fathername_1<<", in "<<subject_1<<"\n\nThe student ,"<<sonname_1<<", is the winner" ;
}
else if (markss_1==marksf_1)
{
cout<<"\nYou ,"<<sonname_1<<", and your father "<<fathername_1<<" have equal marks in
"<<subject_1<<"\n\nIt is a tie ." ;
}
else
{
cout<<"\nThe father, "<<fathername_1<<" ,has more marks than his son ,"<<sonname_1<<", in
"<<subject_1<<"\n\nThe father ,"<<fathername_1<<", is the winner" ;
}
}

void comp::output_2d() {
cout<<"\n\nStudent Father Set 2 (Initialized Using default constructor) : \n\n";
if (markss_2>marksf_2)
{
cout<<"\n\nYou ,"<<sonname_2<<", have more marks than your father ,"<<fathername_2<<",
in "<<subject_2<<"\\nnYou ,"<<sonname_2<<", are the winner" ;
}
else if (markss_2==marksf_2)
{
cout<<"\n\nYou ,"<<sonname_2<<", and your father "<<fathername_2<<" have equal marks in
"<<subject_2<<"\n\nIt is a tie .";
}
else
cout<<"\n\nYour father, "<<fathername_2<<" ,has more marks than you ,"<<sonname_2<<", in
"<<subject_2<<"\n\nYour father ,"<<fathername_2<<", is the winner" ;

void comp::output_4() {

if (s>f)
{
cout<<"\n\nYou ,"<<son<<", have more marks than your father ,"<<father<<", in FSc. ";
}
else if (s==f)
{
cout<<"\n\nYou ,"<<son<<", and your father "<<father<<" have equal marks in FSc. ";
}
else
cout<<"\n\nYour father, "<<father<<" ,has more marks than you ,"<<son<<", in FSc. ";
}

Main Function File(main.cpp)

Code:

#include <iostream>

#include "comp.h"

using namespace std;

int main() {

int m, n;

string h, j;

comp cc;

cc.input();

cc.output();

cc.output_2d();

cout << "\n\nStudent Father Set 3 :\n";

cout << "\nPLease enter your name : ";

cin.get();

getline(cin, h);

cout << "\nPLease enter your father's name : ";

getline(cin, j);

cout << "\nPlease enter your marks in FSc : ";

cin >> m;
cout << "\nPlease enter your father's marks in FSc : ";

cin >> n;

comp dd(m, n, h, j);

dd.output_4();

return 0;

Output:

ASSIGNMENT

GOOGLE DRIVE LINK:

https://drive.google.com/file/d/19nmxQqpCU3xmiw2vzH9yTrONOvkEAAx5/
view?usp=sharing

You might also like