FOP Lab Report 4 - Merged
FOP Lab Report 4 - Merged
Lab Report 04
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;
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.
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.
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.
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:
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
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. ";
}
Code:
#include <iostream>
#include "comp.h"
int main() {
int m, n;
string h, j;
comp cc;
cc.input();
cc.output();
cc.output_2d();
cin.get();
getline(cin, h);
getline(cin, j);
cin >> m;
cout << "\nPlease enter your father's marks in FSc : ";
cin >> n;
dd.output_4();
return 0;
Output:
ASSIGNMENT
https://drive.google.com/file/d/19nmxQqpCU3xmiw2vzH9yTrONOvkEAAx5/
view?usp=sharing