C++ Object-Oriented Programming Lab Guide
C++ Object-Oriented Programming Lab Guide
On
OBJECT ORIENTED
PROGRAMMING
USING C++ LABORATORY
1) Write a program to enter mark of 6 different subjects and find out the total marks.
3
/*Program to find sum total of six different subjects*/
int main()
{
result s1; //object declaration
//[Link]=500;
s1.get_subjects_marks();
s1.calculate_per();
[Link]();
return 0;
}
4
5
2) Write a function using reference variables as arguments to swap the values of pair of
integers
#include<iostream>
int temp;
temp=x;
x=y;
y=temp;
int main()
return 0;
6
3) Write a function to find largest of three numbers
#include<iostream>
return num1;
return num2;
else
return num3;
int main()
cout << "\n The Largest Number: " << largest << endl;
return 0;
8
4) Write a program to find the factorial of a number using Class
#include<iostream>
class Factorial {
private:
int num;
long fact;
public:
void inputNumber() {
cin>>num;
void calculateFactorial() {
start:
if(num < 0) {
goto start;
else if(num == 0) {
9
else {
int i;
fact = 1;
for(i=num;i>=1;i--) {
fact = fact * i;
};
int main() {
Factorial obj;
[Link]();
[Link]();
return 0;
10
5) Define a class to represent a bank account which includes the following members as
Data Members:
Member Functions:
#include<iostream>
class Bank {
private:
string name;
double bal;
public:
[Link]();
cin>>contactNo;
bal = amt;
count++;
acc_no=count;
bal=bal-amt;
else {
void display()
12
{
};
long Bank::count=2019900;
int main()
int amt;
Bank b1;
cin>>amt;
if(amt<1000) {
[Link]();
else {
[Link](amt);
int op;
cout<<endl;
system("pause");
13
do
system("CLS");
cout<<"1. Deposit"<<endl;
cout<<"2. Withdraw"<<endl;
cout<<"4. Exit"<<endl;
cin>>op;
switch(op)
case 1:
cin>>amt;
[Link](amt);
break;
case 2:
cin>>amt;
[Link](amt);
break;
case 3:
[Link]();
break;
14
case 4:
exit(1);
default:
cout<<endl;
system("pause");
15
16
6) Write the above Program for handling n number of account holders using array of
objects.
#include <iostream>
#include <string>
class BankAccount {
private:
string depositorName;
int accountNumber;
double balance;
public:
BankAccount(string name = "", int accNo = 0, double bal = 0.0) { // Constructor to initialize values
depositorName = name;
accountNumber = accNo;
balance = bal;
if (amount > 0) {
balance += amount;
} else {
17
cout << "\n Invalid deposit amount!" << endl;
balance -= amount;
} else {
cout << "\n Depositor Name: " << depositorName << endl;
cout << "\n Account Number: " << accountNumber << endl;
};
int main() {
int n;
cin >> n;
BankAccount accounts[n];
18
// Input details for each account holder
string name;
int accNo;
double balance;
cout << "\n Enter details for Account " << i + 1 << endl;
[Link]();
getline(cin, name);
// Performing operations
double amount;
do {
19
cout << "2. Withdraw Money\n";
switch (choice) {
case 1:
cout << "Enter account index (1 to " << n << "): ";
accounts[accIndex - 1].deposit(amount);
} else {
break;
case 2:
cout << "Enter account index (1 to " << n << "): ";
20
accounts[accIndex - 1].withdraw(amount);
} else {
break;
case 3:
cout << "Enter account index (1 to " << n << "): ";
accounts[accIndex - 1].display();
} else {
break;
case 4:
break;
default:
return 0;
21
22
7) Write a Program to Demonstrate Friend Function
#include<iostream>
class Demo {
int n1,n2;
public:
Demo(int x, int y) {
n1=x;
n2=y;
};
double avg;
avg=((double)obj.n1+obj.n2)/2;
int main() {
Demo d1(7,4);
return 0;
23
24
8) Write a Program to demonstrate Inline Function
#include <iostream>
int main()
int a,b;
cout << "\n Max (4,8): " << Max(4,8) << endl;
cout << "\n Max (6,9): " << Max(6,9) << endl;
cin>>a>>b;
return 0;
25
9) Write a Program to demonstrate Static Members & Functions
#include <iostream>
class Student {
public:
Student() {
cin>>name>>age;
[Link]();
getline(cin,add);
count++;
rollNo=count;
void display() {
};
int Student::count=2450;
int Student::sem=1;
void Student::UpdateSemester() {
sem+=1;
int main(){
[Link]();
Student s2;
[Link]();
[Link]();
[Link]();
return 0;
27
28
10)Write a Program to demonstrate Scope Resolution Operator
/*Program to demonstrate same name local and global variables using Scope Resolution Operator*/
#include <iostream>
int fun1();
void fun2(int);
int main() {
int a=7;
cout<<"\n A= "<<a;
cout<<"\n A= "<<::a;
cout<<"\n A= "<<a;
int fun1() {
a=a+5;
cout<<"\n A= "<<a;
a=a-7;
return(a);
void fun2(int a) {
cout<<"\n A= "<<a;
29
a=a*5;
cout<<"\n A= "<<a;
cout<<"\n A= "<<::a;
30
11) Write a Program to demonstrate Pass by Reference
#include <iostream>
int main() {
int a,b;
cin>>a>>b;
cout<<"\n\t A= "<<a;
cout<<"\n\t B= "<<b;
cout<<"\n\t A= "<<a;
cout<<"\n\t B= "<<b;
return 0;
void swap(int &x, int &y) //x,y will become alias of a,b
int temp;
temp=x; //temp=10
32
12) Write a Program to demonstrate Pass by Value
/*Program to show how pass by value works by swapping two given numbers*/
#include <iostream>
int main() {
return 0;
int temp;
temp=x;
x=y;
y=temp;
33
34
13)Write a Program to demonstrate Constructors
a) Default Constructor
b) Parameterized Constructor
c) Copy Constructor
#include <iostream>
class MyClass {
private:
int num;
public:
};
int main() {
return 0;
35
36
/*Program to demonstrate Parameterized Constructor*/
#include <iostream>
class Rectangle {
int l,b;
public:
l=x;
b=y;
void display() {
};
int main() {
[Link]();
cout<<"\n------------------------------------------------------";
[Link]();
return 0;
37
}
38
/*Program to demonstrate Copy constructor*/
#include <iostream>
class sample {
int a;
public:
a=7;
a=x;
a=obj.a;
void display() {
cout<<"\n\t A: "<<a;
};
int main() {
39
cout<<"\n S1 Object's Value";
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
40
14)Write a Program to demonstrate Destructor
#include <iostream>
int count=10;
class Alpha {
public:
count++;
count--;
};
int main() {
Alpha a3;
return 0;
42
15)Write a Program to demonstrate setprecision() and endl
#include <iostream>
#include <iomanip>
int main() {
cin>>a>>b;
float div;
div=a/b;
return 0;
43
16)Write a Program to define the member function inside and outside the class
/*Program to define the member function inside and outside the class*/
#include <iostream>
class Student {
char name[30];
int age;
float per;
void getdata() {
[Link](name,30);
cin>>age;
cin>>per;
};
class Employee {
string name;
int age;
string des;
public:
44
void getdata() {
[Link]();
getline(cin,name);
cin>>age;
[Link]();
getline(cin,des);
};
int main() {
Student s1;
45
cout<<"\n Enter Student Information\n";
[Link]();
[Link]();
Employee e1;
[Link]();
[Link]();
return 0;
46
17)Write a Program to demonstrate Array of Objects
#include <iostream>
class Student {
string name;
int marks;
public:
void getDetail() {
getline(cin,name);
cin>>marks;
[Link]();
void displayInfo() {
};
int main() {
for(int i=0;i<3;i++) {
47
st[i].getDetail();
for(int i=0;i<3;i++) {
st[i].displayInfo();
return 0;
48
49
18)Write a Program to demonstrate Call by Value
#include <iostream>
int main() {
int a,b;
cout<<"\n\t A= "<<a;
cout<<"\n\t B= "<<b;
cout<<"\n Values of A and B (in main) After Returning from Swap function";
cout<<"\n\t A= "<<a;
cout<<"\n\t B= "<<b;
return 0;
void swap(int x,int y) { //copy of actual arguments are passed to dummy arguments i.e. x,y
temp=x; //temp=10
x=y; //x=5
y=temp; //y=10
50
cout<<"\n\t A= "<<x;
cout<<"\n\t B= "<<y;
51
19)Write a Program to demonstrate Call by Address
#include <iostream>
int main() {
int a,b;
cin>>a>>b;
cout<<"\n\t A= "<<a;
cout<<"\n\t B= "<<b;
cout<<"\n\t A= "<<a;
cout<<"\n\t B= "<<b;
return 0;
void swap(int *x,int *y) { //addresses of actual arguments are received by pointers i.e. *x,*y
int temp;
temp=*x;
*x=*y;
*y=temp;
}
52
53
20)Program to swap private data of two classes using Friend Function
#include <iostream>
class xxx {
private:
int x;
public:
xxx(int xx) {
x=xx;
void display() {
};
class yyy {
private:
int y;
public:
yyy(int yy) {
y=yy;
54
}
void display() {
};
int temp;
temp=objx.x;
objx.x=objy.y;
objy.y=temp;
int main() {
xxx ob1(5);
yyy ob2(4);
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
55
56
21)Write a Program to demonstrate Function Overloading
#include <iostream>
int sum(int,int);
int sum(int,int,int);
double sum(double,double);
double sum(int,double);
double sum(double,int);
int main() {
return 0;
return(p+q);
57
return(p+q+r);
return(p+q);
return(p+q);
return(p+q);
58
22) Write a Program to demonstrate Constructor Overloading
#include <iostream>
class sample {
int a;
public:
a=5;
a=x;
a=x+y;
void display() {
cout<<"\n\t A: "<<a;
};
int main() {
59
cout<<"\n\t\t Demonstrating constructor overloading\n";
[Link]();
[Link]();
[Link]();
return 0;
60
23) Write a Program to demonstrate Inheritance
#include <iostream>
string name;
int age;
public:
void inputDetails(){
getline(cin,name);
cin>>age;
void display(){
};
private:
61
int sub1,sub2;
public:
void inputMarks(){
cin>>sub1;
cin>>sub2;
void studentReport(){
display();
};
int main() {
Report r1;
[Link]();
[Link]();
[Link]();
return 0;
62
63
24)Write a Program to demonstrate Public Inheritance/Derivation
#include <iostream>
class A {
private:
public:
void inputX() {
cin>>x;
int getX() {
return x;
};
int y;
public:
void doubleBase() {
y=2 * getX();
cout<<"\n X: "<<getX();
cout<<"\n Y: "<<y;
64
}
};
int main() {
B b1;
[Link]();
[Link]();
return 0;
65
25)Write a Program to demonstrate Private Inheritance/Derivation
#include <iostream>
private:
public:
void inputX() {
cin>>x;
int getX() {
return x;
};
int y;
public:
y=3*getX();
cout<<"\n X: "<<getX();
cout<<"\n Y: "<<y;
66
}
};
int main() {
B b1;
[Link]();
return 0;
67
26)Write a Program to demonstrate Protected Data Members
#include <iostream>
int x;
public:
void inputX() {
cin>>x;
};
int y;
public:
void compute() {
y=x*x*x;
void display() {
}
68
};
int main() {
B obj;
[Link]();
[Link]();
[Link]();
return 0;
69
27)Write a Program to demonstrate Protected Inheritance/Derivation
#include <iostream>
protected:
public:
void inputX() {
cin>>x;
};
int y;
public:
void compute() {
inputX();
y=x * x * x;
void display() {
}
70
};
int main() {
B obj;
[Link]();
[Link]();
return 0;
71
28) Write a Program to demonstrate Single Inheritance
#include <iostream>
protected:
public:
void inputX(){
cin>>x;
};
private:
public:
void computeArea(){
y=x * x;
};
int main() {
72
Area a1;
[Link]();
[Link]();
return 0;
73
29)Write a Program to demonstrate Multi-Level Inheritance
#include <iostream>
protected:
int x;
public:
void inputX(){
cin>>x;
};
protected:
int y;
public:
void inputY(){
cin>>y;
};
void computeSum(){
z = x + y;
};
int main() {
Sub s1;
[Link]();
[Link]();
[Link]();
return 0;
75
30)Write a Program to demonstrate Multiple Inheritance
#include <iostream>
protected:
int x;
public:
void inputX(){
cin>>x;
};
protected:
int y;
public:
void inputY(){
cin>>y;
};
int z;
76
public:
void sum(){
z = x + y;
void display(){
};
int main() {
C obj;
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
77
31)Write a Program to to Handle Multiple Inheritance Ambiguity
#include <iostream>
protected:
int x;
public:
void inputX(){
cin>>x;
};
protected:
public:
cin>>x;
};
int z;
78
public:
void sum(){
z = A::x + B::x;
void display(){
};
int main(){
C obj;
obj.A::inputX();
obj.B::inputX();
[Link]();
[Link]();
return 0;
79
32)Write a Program to demonstrate Hierarchical Inheritance
#include <iostream>
protected:
public:
x=val;
};
public:
void square(){
y=x * x;
void display(){
};
80
class C:public A{ //2nd Derived class with public derivation
public:
void cube(){
z=x * x * x;
void display(){
};
int main(){
B obj1;
[Link](7);
[Link]();
[Link]();
C obj2;
[Link](8);
[Link]();
[Link]();
return 0;
81
82
33)Consider a publishing company that markets both book and audio cassette version to
its works. Create a class Publication that stores the title (a string) and price (type float)
of a publication. Derive the following two classes from the above Publication class:
Book which adds a page count (int) and Tape which adds a playing time in
minutes(float). Each class should have getdata() function to get its data from the user
at the keyboard. Write the main() function to test the Book and Tape classes by
creating instances of them asking the user to fill in data with getdata() and
then displaying it using putdata().
/*Program to Derive three classes where Book class derived from Publication class and Tape class
derived from Publication class*/
#include <iostream>
protected:
double price;
public:
void getData(){
getline(cin,title);
cin>>price;
};
public:
83
void inputPageCount(){
cin>>pageCount;
void displayBookInfo(){
};
public:
void inputTime(){
cin>>playingTime;
void displayTapeInfo(){
};
int main(){
84
Book b1;
[Link]();
[Link]();
[Link]();
Tape t1;
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
85
86
34)Write a Program to demonstrate Hybrid Inheritance
#include <iostream>
protected:
int x;
public:
void inputX(){
cin>>x;
};
protected:
int y;
public:
void inputY(){
cin>>y;
};
protected:
87
int z;
public:
void inputZ(){
cin>>z;
};
class D : public B, public C { //D is derived from class B and class C (Multiple Inheritance)
public:
void sum(){
};
int main(){
D obj;
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
88
89
35)Write a Program to Resolve Hybrid Inheritance ambiguity Or Diamond Problem
using Virtual Base Class
/*Program to Resolve Hybrid Inheritance ambiguity Or Diamond Problem using Virtual Base Class*/
#include <iostream>
protected:
public:
void inputX(){
cin>>x;
};
protected: //using virtual all the members of A class will come in B class as virtually
public:
void inputY(){
cin>>y;
};
90
protected: //using virtual all the members of A class will come in C class as virtually
public:
void inputZ(){
cin>>z;
};
class D : public B, public C { //D is Derived from class B and class C (Multiple Inheritance)
public:
void sum(){
};
int main(){
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
91
92
36)Write a Program to compute Area of Right Angle Triangle, Equilateral Triangle,
Isosceles Triangle using function overloading
/*Program to compute Area of Right Angle Triangle, Equilateral Triangle, Isosceles Triangle using
Function Overloading*/
#include <iostream>
#include <math.h>
float area(int);
float area(int,int);
float area(float,float);
int main(){
float ar,ht,bs;
int s1,s,b;
cin>>s1;
cout<<"-----------------------------------------------------------";
//-----------------------------------------------------------------------------------------------------------------
-
cout<<"\n Input the Side and Base of the Isosceles Triangle: ";
cin>>s>>b;
cout<<"-----------------------------------------------------------";
93
//-----------------------------------------------------------------------------------------------------------------
-
cin>>ht>>bs;
cout<<"-----------------------------------------------------------";
return 0;
//-----------------------------------------------------------------------------------------------------------------
-
return res;
//-----------------------------------------------------------------------------------------------------------------
-
return ans;
//-----------------------------------------------------------------------------------------------------------------
-
return ans;
//-----------------------------------------------------------------------------------------------------------------
-
95
37)Consider an example of declaring the examination result. Design three classes student,
exam and result. The student has data members such as rollno, name. Create the class
exam by inheriting the student class. The exam class adds data members representing
the marks scored in 5 subjects. Derive the result from exam-class and it has own data
members like total, average.
#include <iostream>
protected:
int rollNo;
string name;
public:
void inputData() {
getline(cin,name);
cin>>rollNo;
};
protected:
int s1,s2,s3,s4,s5;
public:
void getSubjectMarks() {
96
cout<<" Enter Marks of Five Subjects: ";
cin>>s1>>s2>>s3>>s4>>s5;
};
private:
int total;
double avg;
public:
void CalculatePer() {
total=s1+s2+s3+s4+s5;
avg=((double)total/500)*100;
void display() {
};
int main() {
[Link]();
97
[Link]();
[Link]();
[Link]();
return 0;
98
38)Write a Program to demonstrate Unary Operator Overloading using Member
Function
#include <iostream>
class sample {
char c;
public:
a=4;
b=6;
c='X';
a=a+1;
b=b+1;
a=a+1;
b=b+1;
99
}
void display(){
};
int main(){
[Link]();
[Link]();
++s1; //[Link]++();
s1++; //[Link]++(0);
++s2; //[Link]++();
[Link]();
[Link]();
return 0;
100
39)Write a Program to demonstrate Unary Operator Overloading using Friend Function
#include <iostream>
class sample{
public:
a=54;
b=57;
obj.a=obj.a+1;
obj.b=obj.b+1;
void display(){
};
int main(){
[Link]();
101
[Link]();
++s1; //operator++(s1);
++s2; //operator++(s2);
++s2; //operator++(s2);
cout<<" S1 Object"<<endl;
[Link]();
cout<<" S2 Object"<<endl;
[Link]();
return 0;
102
40)Write a Program to demonstrate Binary Operator Overloading using Member
Function
#include <iostream>
class Sample {
int a,b;
public:
a=0;
b=0;
a=x;
b=y;
Sample temp;
temp.a = a + obj.a;
temp.b = b + obj.b;
return temp;
void display(){
103
}
};
int main(){
Sample s1(15,15),s2(24,30),s3;
[Link]();
[Link]();
[Link]();
return 0;
104
41)Write a Program to demonstrate Binary Operator Overloading using Friend Function
#include <iostream>
class Sample {
int a,b;
public:
a=0;
b=0;
a=x;
b=y;
Sample temp;
return temp;
void display(){
}
105
};
int main(){
Sample s1(20,15),s2(28,60),s3;
[Link]();
[Link]();
s3 = s1 + s2; //operator+(s1,s2);
[Link]();
return 0;
106
42)Write a Program to Concatenate Two Strings Objects using Binary Operator (+)
Overloading
#include <iostream>
#include <string.h>
class AddString {
string str;
public:
AddString(){
str = "";
AddString(string temp){
str = temp;
AddString temp;
return temp;
void display(){
cout<<str;
};
107
int main(){
cin>>str1;
cin>>str2;
AddString a1(str1);
AddString a2(str2);
a3 = a1 + a2; //[Link]+(a2);
return 0;
108
43)Write a Program of Operator Overloading when Friend function is Compulsory
#include <iostream>
class Sample {
int a,b;
public:
a=0;
b=0;
a=x;
b=y;
Sample temp;
return temp;
void display(){
}
109
};
int main(){
Sample s1(21,14),s2;
[Link]();
[Link]();
return 0;
110
44)Write a Program to illustrate concept of Virtual Functions
#include <iostream>
public:
};
public:
void show(){
};
public:
void show(){
};
int main() {
ptr = &b1;
ptr->show();
Derived1 d1;
ptr = &d1;
Derived2 d2;
ptr = &d2;
return 0;
112
45)Write a Program to demonstrate Runtime Polymorphism
#include <iostream>
#include <conio.h>
class Shape {
protected:
public:
width=x;
height=y;
};
public:
void area(){
};
public:
void area(){
};
Shape *shape;
Rectangle rec(5,6);
Triangle tri(10,12);
int op;
cout<<"\n Enter 1 to Calculate Area of Rectangle OR\n"" Enter 2 to Calculate Area of Triangle:
";
cin>>op;
shape = &rec;
shape = &tri;
114
}
else {
shape->area();
return 0;
115
46)Write a Program to demonstrate Manipulators
#include <iostream>
#include <iomanip>
int main() {
cout << setw(10) << left << "Dog Age" << "|";
cout << setw(12) << right << "Human Age" << endl;
cout << setw(10) << left << "2 Months" << "|";
cout << setw(12) << right << "14 Months" << endl;
cout << setw(10) << left << "6 Months" << "|";
cout << setw(12) << right << "5 Years" << endl;
cout << setw(10) << left << "8 Months" << "|";
cout << setw(12) << right << "9 Years" << endl;
cout << setw(10) << left << "1 Year" << "|";
cout << setw(12) << right << "15 Years" << endl;
116
// Produce Long line
return 0;
117
47)Write a Program to Create (Open and Close) a Empty File
#include <iostream>
#include <fstream>
int main() {
fstream new_file;
if(!new_file){
else {
return 0;
118
48)Write a Program to Write data into a File
#include <iostream>
#include <fstream>
int main() {
ofstream new_file;
new_file.open("[Link]");
if(!new_file){
else{
return 0;
119
49)Write a Program to Read data from a File
#include <iostream>
#include <fstream>
int main() {
ifstream new_file;
new_file.open("[Link]");
if(!new_file){
else{
char str[40];
while(!new_file.eof()){
cout<<str<<endl;
new_file.close();
return 0;
120
50)Write a Program to Append data into a File
#include <iostream>
#include <fstream>
int main() {
ofstream new_file;
if(!new_file) {
else {
new_file.close();
return 0;
121
122