OOP Lab Manual Solution
OOP Lab Manual Solution
OBJECT-ORIENTED PROGRAMMING
LAB MANUAL
SOLUTION
University Of Lahore
CS-2203 OOP LAB Manual
Page 2 of 38
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:
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>
{
int result;
result=a+b+c;
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();
}
1 is given as an input 16
Problem Statement 2:
PROGRAM
#include<conio.h>
#include<iostream.h>
{
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();
}
Problem Statement 3:
PROGRAM
#include<conio.h>
#include<iostream.h>
void output()
{
for(int i=0;i<=5;i++)
{
cout<<”*”;
}
}
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:
Problem Statement 1:
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();
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;
cout<<city<<endl;
}
};
void main()
{
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();
}
}
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
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
Lab Task 3
Topics covered:
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];
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");
}
Problem Statement 2:
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++)
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);
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>
#include<math.h>
private:
float d;
public:
AB() // constructor for class AB
{
cout<<"Enter the first distance(in feet):";
cin>>d;
}
class AC
private:
float d;
public:
cin>>d;
}
};
float total;
b.d=b.d/12;
total=a.d+b.d;
cout<<"Total Distance:"<<total<<"feet";
void main()
clrscr();
AB A;
AC B;
add(A,B);
getch();
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;
Problem Statement 3:
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)
{
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
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:
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()
{
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:
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()
{
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.
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;
}
};
}
};
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
Lab Task 9
Topics covered:
Virtual Functions
Problem Statement :
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 : ";
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()
{
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 :
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
return(0);
}
Lab Task 11
Topics covered:
Exception Handling
Problem Statement :
Lab Task 12
Topics covered:
Operator Overloading
Problem Statement :
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();
c.set();
e.set();
d=b+c;
d=d-e;
d.output();
getch();
}