Page 1 of 38
OBJECT-ORIENTED PROGRAMMING
LAB MANUAL
SOLUTION
University Of Lahore
CS-2203 OOP LAB Manual
Page 2 of 38
Department of Computer Science and IT
OBJECT ORIENTED PROGRAMMING LAB MANUAL
Aim:
To develop object-oriented programming skills using C++.
Helpful for students to start with their concepts of Computer
Programming and leading to OOP.
Lab Task 1:
Topics Covered:
Default parameters, Inline Functions and Function overloading
Problem Statement 1:
Write a function ‘sum’ to add the values for three variables and
store the result in another variable. Function will take 3
arguments as default. Call the function in main with 1, 2 and then
3 variables.
PROGRAM
#include<conio.h>
#include<iostream.h>
void sum( int a=3, int b =6 int c=9)
{
int result;
result=a+b+c;
cout<<”sum of three variables is “<<result;
}
CS-2203 OOP LAB Manual
Page 3 of 38
void main()
{
clrscr();
sum(); take 3, 6 and 9 as default value
sum(1); take 6 and 9 as default
sum(2,3); take 9 as default
getch();
}
Sample input and output
Sample Input Sample Output
No input for first function. 18
1 is given as an input 16
2 and 3 are given as input 14
Problem Statement 2:
CS-2203 OOP LAB Manual
Page 4 of 38
Write a program that inputs radius of circle and uses an inline
function AREA() to calculate and return the area of circle.
PROGRAM
#include<conio.h>
#include<iostream.h>
inline float AREA(float a)
{
float b;
b=3.14*a*a;
return b;
}
void main()
{
clrscr();
float rad;
cout<<”enter radius”;
cin>>rad;
cout<<”Area of circle”<<AREA(rad);
getch();
}
Sample Input Sample Output
4 50.24
CS-2203 OOP LAB Manual
Page 5 of 38
Problem Statement 3:
Write three functions ‘output’. The first version takes no
parameters and display 5 asterisk. The second version takes an
integer and display the number of i intergers. The third version
takes an integer and a character as parameters and displays the
number of n characters.
PROGRAM
#include<conio.h>
#include<iostream.h>
void output()
{
for(int i=0;i<=5;i++)
{
cout<<”*”;
}
}
void output( int n )
{
for(int i=0;i<n;i++)
{
cout<<i;
}
}
void output(int n , char b)
{
for(int i=0;i<=n;i++)
{
cout<<b;
}
}
void main()
{
clrscr();
CS-2203 OOP LAB Manual
Page 6 of 38
output();
output(4);
outpu(3,’A”);
getch();
}
Outputs
*****
0123
AAA
Lab Task 2
Topics covered:
CS-2203 OOP LAB Manual
Page 7 of 38
Create classes and objects
Implement classes using member functions
Problem Statement 1:
Write a program to input date(day,month,year) by a member function and
print the date(day,month,year) by writing the another member function.
PROGRAM
#include<iostream.h>
#include<conio.h>
class Date
{
private:
int year,month,day;
public:
void setDate( )
{
cout<<"Enter Year ";
cin>>year;
cout<<"Enter Month";
cin>>month;
cout<<"Enter Date";
cin>>day;
}//end of setDate
void printDate()
{
cout<<"Today Date is......"<<day<<"/"<<month<<"/"<<year;
}//end of printDate
};//end of Date
Void main()
{
Date d;
d.setDate();
d.printDate();
CS-2203 OOP LAB Manual
Page 8 of 38
Sample Input Sample Output
Year : 2001, Month: 12, Day: 23 23/12/2001
Problem Statement 2:
Write a program by using a class that takes name, age and city of a person as
class attributes. An input Details member functions to input the data, getAge
function to return age and Display function to display name , age and city.Input
the data for two persons and display the record of the person who is elder in age.
PROGRAM
#include<iostream.h>
#include<conio.h>
class person{
private:
char name[25];
char city[20];
int age;
public:
void input()
{
cout<<"The name of Person \n";
cin.getline(name,25);
cout<<"The age of Person \n ";
cin>>age;
cout<<"The City of Person \n";
cin.ignore();
cin.getline(city,20);
}
int getage()
{
cout<<age;
void display()
{
cout<<name<<endl;
cout<<age<<endl;
CS-2203 OOP LAB Manual
Page 9 of 38
cout<<city<<endl;
}
};
void main()
{
cout<<"Here You Go..............!!!!!!!!!!!!!! \n";
person p1,p2;
int age1,age2;
p1.input();
cout<<endl;
cout<<"The record of Second person \n";
p2.input();
cout<<endl;
age1=p1.getage();
age2=p2.getage();
system("pause");
if(age1>age2)
{
p1. display();
}
if(age2>age1)
{
p2.display();
}
}
Input for person 1 Input for person 2 Output
Ali , 23, Lahore. Hassan , 20 , Jhang Ali , 23, Lahore
////////elder in age
Problem Statement 3:
Write a class of Student that has age as data members. A constructor with one
parameter initializes data members input value if input value is>7 and<30
with given value
CS-2203 OOP LAB Manual
Page 10 of 38
Else displays message:
“You have entered Invalid value”
and member function show displays the age of student
. You have to create two objects and display the values inside them.
PROGRAM
#include<iostream.h>
class Student
{
private:
int age;
public:
Student(int a) // constructor with one parameter
{
if(a>7&& a<30)
{
age=a;
}//end of if
else
{
cout<<"You input Invalid value"<<endl;
}//end of else
}//end of constructor function
void show_age()
{
cout<<"Age of student is"<<age<<endl;
}
};//end of class
void main()
{
Student s1(3); //input wrong value
s1.show_age(); //it will show garbage
cout<<"\n\n";
Student s2(9);
s2.show_age(); //it will show 9
}//end of main
CS-2203 OOP LAB Manual
Page 11 of 38
Lab Task 3
Topics covered:
Create class using Default and Parameterized Constructors
Problem Statement 1:
Write a class Cricket that contains attributes for the player’s name ,
average and team .
Write three functions to input, change and display these attributes. Also
write a constructor to initialize the values.
PROGRAM
#include<iostream>
#include<windows.h>
#include<string.h>
using namespace std;
class cricket
{
private :
char pname[50];
double score;
char team[25];
public :
void input()
{
cout<<"Name : ";
cin.getline(pname,50);
cout<<"Score : ";
cin>>score;
cout<<"Team : ";
cin.ignore();
cin.getline(team,25);
}
void change( char n[] , double s , char t[])
{
int i,j;
for( i=0 ; n[i]!='\0' ; i++)
pname[i]=n[i];
CS-2203 OOP LAB Manual
Page 12 of 38
pname[i]='\0';
for( j=0 ; n[j]!='\0' ; j++)
team[j]=t[j];
team[j]='\0';
score=s;
}
void display()
{
cout<<"Name : "<<pname<<endl;
cout<<"Score : "<<score<<endl;
cout<<"Team : "<<team<<endl;
}
cricket()
{
pname[0]='\0';
team[0]='\0';
score=0;
}
};
int main()
{
cricket pak;
char n[50];
char t[25];
double s;
pak.input();
pak.display();
cout<<"\t\t\t ---------- \n";
cout<<"Name : ";
cin.getline(n,50);
cout<<"Score : ";
cin>>s;
cout<<"Team : ";
cin.ignore();
cin.getline(t,25);
pak.change( n , s , t);
pak.display();
system("pause");
}
Sample Input Sample Output: Sample Input for Sample Output
change function after change
iram , 50 , iram, 50, anam,30, india anam,30 ,india
Pakistan Pakistan
CS-2203 OOP LAB Manual
Page 13 of 38
Problem Statement 2:
Define a class “Bank: that includes the following data members:
Name of Depositor
Account Number
Balance Amount
The class also contains the following member functions:
A parameterized constructor to initialize :
Name->Ahmed
Balance Amount->100
Account Number->123
Deposit Function to deposit some amount. It should accept amount as
a parameter.
Withdraw function to withdraw an amount. It should also accept
amount as a parameter.
Display function to display name and balance amount after deposit
and withdraw.
PROGRAM
#include<iostream>
#include<windows.h>
#include<string.h>
using namespace std;
class bank
{
private :
char name[25];
double money;
char account[10];
public :
bank( char n1[] , double m , char a[])
{
int i,j;
for( i=0 ; n1[i]!='\0' ; i++)
name[i]=n1[i];
name[i]='\0';
for( j=0 ; a[j]!='\0' ; j++)
CS-2203 OOP LAB Manual
Page 14 of 38
account[j]=a[j];
account[j]='\0';
money=m;
}
void deposit(double amout )
{
money=money+amout;
cout<<"Now Balance is : "<<money<<endl;
}
void withdraw(double amout )
{
money=money-amout;
cout<<"Withdraw\n";
cout<<"Now Balance is : "<<money<<endl;
}
void dispaly()
{
cout<<"Name : "<<name<<endl;
cout<<"Balance : "<<money<<endl;
cout<<"Acount # : "<<account<<endl;
}
};
int main()
{
double amout;
bank mine("Hassan", 100, "123" );
mine.dispaly();
cout<<"Enter The amount to deposit : ";
cin>>amout;
mine.deposit(amout);
cout<<"Enter The amount to withdraw : ";
cin>>amout;
mine.withdraw(amout);
cout<<"Dispaly Function \n";
mine.dispaly();
system("pause");
}
Sample Initialized Sample Output Balance Sample Output Balance
Input amount after deposit amount after withdraw
Hassan, 100,123 100+20=120 120-30=90
CS-2203 OOP LAB Manual
Page 15 of 38
Lab Task 4
Topics covered:
Describe the structure of friend functions and friend classes.
Use the practical concept of friend functions and friend classes
Problem Statement 1:
To write a c++ program to read a value of distance from one object and add
with a value in another object using friend function.
PROGRAM
#include<iostream.h>
#include<conio.h>
CS-2203 OOP LAB Manual
Page 16 of 38
#include<math.h>
class AC; // Declaration of class AC;
class AB
{
private:
float d;
public:
AB() // constructor for class AB
{
cout<<"Enter the first distance(in feet):";
cin>>d;
}
friend void add(AB,AC); //declaration of friend function
};
class AC
private:
float d;
public:
AC() // constructor for class AC
cout<<"Enter the second distance(in inches):";
cin>>d;
}
};
void add(AB a,AC b) // definition of friend function
float total;
b.d=b.d/12;
CS-2203 OOP LAB Manual
Page 17 of 38
total=a.d+b.d;
cout<<"Total Distance:"<<total<<"feet";
void main()
clrscr();
AB A;
AC B;
add(A,B);
getch();
Sample Input 1 Sample Input 2 Sample Output
Enter the first distance Enter the second distance Total Distance: 14.5feet
(in feet): 12 (in inches): 30
Problem Statement 2:
To write a c++ program using friend class to find the sum of two numbers.
PROGRAM
#include<iostream.h>
#include<conio.h>
class readint
{
float a,b;
public:
void read()
{
cout<<"\n\nEnter the First Number : ";
cin>>a;
CS-2203 OOP LAB Manual
Page 18 of 38
cout<<"\n\nEnter the Second Number : ";
cin>>b;
}
friend class sum;
};
class sum
{
public:
float c;
void add(readint rd)
{
c=rd.a+rd.b;
cout<<"\n\nSum="<<c;
}
};
void main()
{
int cont;
readint rd;
sum s;
clrscr();
do
{
clrscr();
rd.read();
s.add(rd);
cout<<"\n\nDo you want to continue?(1-YES,0-NO)";
cin>>cont;
}while(cont==1);
getch();
}
Sample Input 1 Sample Input 2 Sample Output
Enter the first number: 12 Enter the second number: Sum= 26
14
CS-2203 OOP LAB Manual
Page 19 of 38
Problem Statement 3:
Create a class ”data” with attribute distance in centimeter.
Create another class “Data2” that will be a friend class of “data” and it
should have the following attributes and member functions:
Length as an attribute
Input function to take the input of distance and length.
Check function to check the values of length and sitance at point’0’.
Increment the value by 1 if any of the value or both the values are zero before
adding.
PROGRAM
#include<iostream.h>
class data2;
class data
{
friend class data2;
private:
int dist;
};
class data2
{ private:
int len;
public:
data d;
void input()
{
cout<<"enter distance";
cin>>d.dist;
cout<<"enter length";
cin>>len;
}
void add()
{ int res;
res=d.dist+len;
cout<<"addition of length and distance is"<<res;
}
void check()
{ if(d.dist==0)
d.dist++;
else if(len==0)
len++;
else if(len==0&&d.dist==0)
{
CS-2203 OOP LAB Manual
Page 20 of 38
len++;
d.dist++;
}}};
main()
{ data d1;
data2 d2;
d2.input();
d2.check();
d2.add();
}
Sample Sample
Input Output
Length=2 Output=2+4=6
Distance=4
CS-2203 OOP LAB Manual
Page 21 of 38
Lab Task 6
Topics covered:
Describe the structure of base and derived classes
Types of Inheritance
Problem Statement 1:
Create a class Employee with the following attributes and member functions:
Employee registeration number
Employee name
Destination
Human resource Allowance
Basic salary
Profitable fund
Get function to take input of Registeration number, name and
destination.
Inherit(public) a class Salary from employee . Salary should contain the
following member functions:
Get function to take input of human resource allowance, basic salary
and profitable fund.
PROGRAM
#include<iostream>
#include<windows.h>
using namespace std;
class employee
{
protected:
char name[20];
char address[75];
int number;
int basic_pay;
int allownce;
int fund;
public:
CS-2203 OOP LAB Manual
Page 22 of 38
employee()
{
name[0]='\0';
address[0]='\0';
number=0;
basic_pay=0;
allownce=0;
fund=0;
}
void epm_input()
{
cout<<"Enter Name : ";
cin.getline(name,20);
cout<<"Enter Destination : ";
cin.getline(address,75);
cout<<"Enter Number : ";
cin>>number;
cout<<endl;
}
};
class salry : public employee
{
public :
void salry_input()
{
cout<<"Enter Basic Pay : ";
cin>>basic_pay;
cout<<"Enter Allownce : ";
cin>>allownce;
cout<<"Enter Fund : ";
cin>>fund;
}
void sum()
{
cout<<"Sum = "<<basic_pay+allownce+fund<<endl;
}
void out_put()
{
cout<<name<<endl<<address<<endl<<number<<endl<<basic_pay<<endl<<allownce<<endl<<f
und<<endl;
}
};
int main()
{
salry man1;
CS-2203 OOP LAB Manual
Page 23 of 38
man1.epm_input();
man1.salry_input();
man1.out_put();
man1.sum();
system("pause");
}
Sample Sample
Input Output
Name : ali
Employee
number: 2 Output=60
Destination:
Associate
Pay=20,20,20
Problem Statement 2:
CS-2203 OOP LAB Manual
Page 24 of 38
Implement the above problem with protected inheritance.
PROGRAM
#include<iostream>
#include<windows.h>
using namespace std;
class employee
{
protected:
char name[20];
char address[75];
int number;
int basic_pay;
int allownce;
int fund;
void epm_input()
{
cout<<"Enter Name : ";
cin.getline(name,20);
cout<<"Enter Destination : ";
cin.getline(address,75);
cout<<"Enter Number : ";
cin>>number;
cout<<endl;
}
};
class salry : protected employee
{
public :
//employee::employee();
// employee::epm_input();
void salry_input()
{
employee::epm_input();
cout<<"Enter Basic Pay : ";
cin>>basic_pay;
cout<<"Enter Allownce : ";
cin>>allownce;
cout<<"Enter Fund : ";
cin>>fund;
}
void sum()
{
CS-2203 OOP LAB Manual
Page 25 of 38
cout<<"Sum = "<<basic_pay+allownce+fund<<endl;
}
void out_put()
{
cout<<name<<endl<<address<<endl<<number<<endl<<basic_pay<<endl<<allownce<<endl<<f
und<<endl;
}
};
int main()
{
salry man1;
man1.salry_input();
man1.out_put();
man1.sum();
system("pause");}
Sample Input SampleOutput
Name : ali
Employee
number: 2
Destination: Output=60
Associate
Pay=20,20,20
Lab Task 7
CS-2203 OOP LAB Manual
Page 26 of 38
Topics covered:
Multilevel Inheritance
Problem Statement :
To write a c++ program to get student details, total marks & average
marks using Multilevel Inheritance.
Declare the base class student.
Define three data members rollno, marks1 & marks2.
Declare and define the function get() to get input for data members
rollno, marks1 & marks2.
Declare the other class arts.
Define one data member arts marks.
Declare and define the function getam() to input the arts mark.
Create the class result derived from student and arts.
Declare and define the function display() to find out the total and
average.
Declare the derived class object & call the functions get(),getam() and
display().
Stop the program
PROGRAM
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
CS-2203 OOP LAB Manual
Page 27 of 38
class sports:public student
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
Student::get();
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
}; Sample PROGRAM OUTPUT:
void main()
{ Enter the Roll no: 100
clrscr(); Enter two marks: 90, 80
statement obj; Enter the Arts Mark: 90
obj.getsm();
obj.display(); Roll No: 100
getch(); Total : 260
} Average: 86.66
CS-2203 OOP LAB Manual
Page 28 of 38
Lab Task 9
Topics covered:
Virtual Functions
Problem Statement :
Calculate the area of triangle, circle and polygon using virtual
functions, also show the largest area of three of them.
PROGRAM
#include<iostream>
#include<windows.h>
using namespace std;
class area_cal
{
protected:
float area;
float width;
float lenght;
public :
virtual int cal()
{
}
virtual void input()
{
}
virtual void show()
{
}
};
class squr: public area_cal
{
public :
void input()
{
cout<<"Enter Width : ";
cin>>width;
cout<<"Enter Lenght : ";
CS-2203 OOP LAB Manual
Page 29 of 38
cin>>lenght;
}
squr()
{
width=0;
lenght=0;
}
int cal()
{
area=width*lenght;
}
void show()
{
cout<<"Area Of square : "<<area;
}
};
class tri: public area_cal
{
public :
void input()
{
cout<<"Enter Base : ";
cin>>width;
cout<<"Enter Height : ";
cin>>lenght;
}
tri()
{
width=0;
lenght=0;
}
int cal()
{
area=width*lenght/0.5;
}
void show()
{
cout<<"Area Of square : "<<area;
}
};
int main()
CS-2203 OOP LAB Manual
Page 30 of 38
{
int num1;
int num2;
area_cal *ptr;
squr a1;
tri b1;
ptr=&a1;
cout<<"Square Area \n";
ptr->input();
num1=ptr->cal();
ptr->show();
ptr=&b1;
cout<<"Triangle Area \n";
ptr->input();
num2=ptr->cal();
ptr->show();
if(num1>num2)
cout<<"Square is big\n";
else
cout<<"Triangle is Big\n";
system("pause");
}
Lab Task 10
CS-2203 OOP LAB Manual
Page 31 of 38
Topics covered:
Aggregation
Problem Statement :
Implement aggregation on employee -company relationship.
PROGRAM
#include<iostream.h>
class Employee
{
public:
Employee(char *name){
cout<<"Employee::ctor\n";
myName_p = new char(sizeof(strlen(name)));
myName_p = name;
}
char* disp(){return(myName_p);};
~Employee()
{
cout<<"Employee:dtor\n\n";
delete (myName_p);
}
private:
char *myName_p; }; //continued to next slide…
class Company
{
public:
Company(char * compName, Employee* emp){
cout<<"Company::ctor\n";
name = new char(sizeof(strlen(compName)));
name = compName;
myEmp_p = emp;
};
~Company()
{
cout<<"Company:dtor\n\n";
CS-2203 OOP LAB Manual
Page 32 of 38
myEmp_p = NULL;
};
private:
char *name;
Employee *myEmp_p;
};
int main()
{
cout<<"\nExample of Aggregation Relationship \n";
cout<<"-----------------------------------------\n\n";
{
cout<<"Here, an Employee-Ali works for Company-MS \n";
Employee emp(“Ali");
{
Company comp("MS", &emp);
} // here Company object will be deleted, whereas Employee object is still there
cout<<"At this point Company gets deleted...\n";
cout<<"\nBut Employee-"<<emp.disp();
cout<<" is still there\n";
} //here Employee object will be deleted
return(0);
}
Lab Task 11
Topics covered:
CS-2203 OOP LAB Manual
Page 33 of 38
Exception Handling
Problem Statement :
Implement stack push and pop with exception handling
CS-2203 OOP LAB Manual
Page 34 of 38
CS-2203 OOP LAB Manual
Page 35 of 38
CS-2203 OOP LAB Manual
Page 36 of 38
CS-2203 OOP LAB Manual
Page 37 of 38
Lab Task 12
Topics covered:
Operator Overloading
Problem Statement :
Solve the expression “b+c-e” with operator overloading
PROGRAM
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class add
{
private:
int a;
public:
void set()
{ Sample Output
cout<<"Enter the integers......:";
cin>>a; Input: 2, 3 4
}
add operator +(add p) Output : 1
{
add temp;
temp.a=a+p.a;
return temp;
}
add operator -(add o)
{
add result;
result.a=a-o.a;
return result;
}
void output()
{
cout<<a;
}
};
main()
{
add b,c,d,e;
b.set();
CS-2203 OOP LAB Manual
Page 38 of 38
c.set();
e.set();
d=b+c;
d=d-e;
d.output();
getch();
}
CS-2203 OOP LAB Manual