1
1. Develop a C++ program to overload the addition operator (+) to add two
complex numbers
#include<iostream>
using namespace std;
class Complex
public:
int real;
int img;
Complex operator+(Complex c)
Complex temp;
temp.real=real+c.real;
temp.img=img+c.img;
return temp;
};
int main()
Complex c1,c2,c3;
c1.real=5; c1.img=3;
c2.real=10;c2.img=5;
c3=c1+c2;
cout<<c3.real<<"+i"<<c3.img<<endl;
return 0;
OUTPUT: 15+i8
2
2. Develop a C++ program to implement hybrid inheritance
#include<iostream>
using namespace std;
class A
public :
A()
cout<<"class A"<<endl;
};
class B
public :
B()
cout<<"class B"<<endl;
};
class C : public A
public :
C()
cout<<"class C"<<endl;
};
class D : public A,public B
{
3
public :
D()
cout<<"class D";
};
int main()
D obj;
OUTPUT:
class A
class B
class D
4
3. Develop a C++ program to overload a operator ‘+’ to add two time objects
which consists of hr, min and sec as data members
#include<iostream>
using namespace std;
class Time
public:
int hr;
int min;
int sec;
Time operator+(Time t)
Time temp;
int a,b;
a=sec+t.sec;
temp.sec=a%60;
b=min+t.min+(a/60);
temp.min=(b%60);
temp.hr=hr+t.hr+(b/60);
temp.hr=temp.hr;
return temp;
};
int main()
Time t1,t2,t3;
t1.hr=5;
5
t1.min=20;
t1.sec=35;
t2.hr=8;
t2.min=46;
t2.sec=65;
t3=t1+t2;
cout<<t3.hr<<"hours "<<t3.min<<"mins "<<t3.sec<<"seconds "<<endl;
return 0;
OUTPUT: 14hours 7mins 40seconds
6
4. Develop a C++ program to overload constructor (take one constructor as
copy constructor)
#include<iostream>
using namespace std;
class A
{
public :
int a;
A()
{
a=10;
}
A(int c,int b)
{
a=b+c;
}
A(A &obj)
{
a=obj.a;
}
void disp()
{
cout<<a<<endl;
}
};
int main()
{
A obj1(10,20);
A obj2(obj1);
obj1.disp();
obj2.disp();
}
OUTPUT:
30
30
7
5. Develop a C++ program to overload ‘-‘ operator to subtract two complex
numbers
#include<iostream>
using namespace std;
class Complex{
public:
int real;
int img;
Complex operator-(Complex c)
Complex temp;
temp.real=real-c.real;
temp.img=img-c.img;
return temp;
};
int main()
Complex c1,c2,c3;
c1.real=5;
c1.img=3;
c2.real=10;
c2.img=5;
c3=c1-c2;
cout<<c3.real<<"+i("<<c3.img<<")"<<endl;
return 0;
OUTPUT: -5+i(-2)
8
6. Develop a C++ program to implement multiple inheritance
#include<iostream>
using namespace std;
class A
public :
A(){cout<<"class A\n";}
};
class B
public :
B(){cout<<"class B\n";}
};
class C : public A,public B
public :
C(){cout<<"class C";}
};
int main()
C obj;
OUTPUT:
class A
class B
class C
9
7. Develop a C++ Program to display names, roll no’s, and grades of 3 students
who have appeared in the examination. Declare the class of name, roll no’s and
grade. Create an array of class objects. Read and display the contents of the
array.
#include<iostream>
using namespace std;
class student{
string name;
char grade;
int rollno;
public:
void display()
cout<<rollno;
cout<<"\t"<<name;
cout<<"\t"<<grade;
cout<<endl;
void getdata(string a,char b,int c)
name=a;grade=b;rollno=c;
};
int main()
student o[3];
o[0].getdata("vikas",'A',1);
o[1].getdata("shiva",'B',2);
o[2].getdata("venu",'C',3);
cout<<"roll no\t"<<"name\t"<<"grade"<<endl;
o[0].display();
10
o[1].display();
o[2].display();
OUTPUT:
roll no name grade
1 vikas A
2 shiva B
3 venu C
11
8. Develop a C++ program to overload unary ‘-‘ operator to apply on object
#include<iostream>
using namespace std;
class complex
public:
float real,imag;
complex()
{ real=0;imag=0;}
complex(float r, float i)
{ real=r; imag=i; }
void display()
cout<<real<<"+i("<<imag<<")"<<endl;
void operator-()
real=-real;
imag=-imag;
};
int main()
complex c1(-2.3,4.5);
c1.display();
-c1;
c1.display();
OUTPUT: 2.3+i(-4.5)
12
9. Develop a C++ program to read the data of N number of employees and
compute Net salary of each employee (DA=52% of Basic and Income Tax (IT)
=30% of the gross salary
(Hint: data members:- empno, empname, basic
HRA Member functions:- readEmp(), compNet(), Disp()
Gross = Basic + DA +HRA, Net = Gross – Income tax)
#include<iostream>
using namespace std;
class company{
string empname;
int empno,basic,hra;
float it,net,gross,da;
public:
void reademp(string a,int b,int c,int d)
empname=a;empno=b;basic=c;hra=d;
void compNet()
da=(0.52)*basic;
gross=basic+da+hra;
it=(0.3)*gross;
net=gross-it;
void disp()
cout<<empno<<"\t";
cout<<empname<<"\t";
cout<<hra<<"\t";
cout<<gross<<"\t";
cout<<it<<"\t";
13
cout<<net<<"\t";
cout<<endl;
};
int main()
int n,i,b,c,d;string a;
cout<<"enter no. of employees";
cin>>n;
company E[n];
for(i=0;i<n;i++)
cout<<"Enter employee details:employee name,no,basic,hra\n";
cin>>a>>b>>c>>d;
E[i].reademp(a,b,c,d);
E[i].compNet();
cout<<"empno\t"<<"empname\t"<<"hra\t"<<"gross\t"<<"it\t"<<"net salary\t"<<endl;
for(i=0;i<n;i++)
E[i].disp();
}
14
OUTPUT:
enter no. of employees3
Enter employee details:employee name,no,basic,hra
nan 3 100000 5000
Enter employee details:employee name,no,basic,hra
nan 3 100000 5000
Enter employee details:employee name,no,basic,hra
nan 3 100000 5000
empno empname hra gross it net salary
3 nan 5000 157000 47100 109900
3 nan 5000 157000 47100 109900
3 nan 5000 157000 47100 109900
15
10. Develop a C++ program to illustrate function overloading concept
#include<iostream>
using namespace std;
int add(int x,int y)
return x+y;
int add(int x,int y,int z)
return x+y+z;
double add(double x,double y)
return x+y;
int main()
cout<<"addition of 4,5="<<add(4,5)<<endl;
cout<<"addition of 2,3,4= "<<add(2,3,4)<<endl;
cout<<"addition of 4.5,3.4="<<add(4.5,3.4)<<endl;
OUTPUT:
addition of 4,5=9
addition of 2,3,4= 9
addition of 4.5,3.4=7.9
16
11. Develop a C++ program to create multilevel inheritance. (Hint:classes A1,A2,
A3)
#include<iostream>
using namespace std;
class A1
public :
A1()
{cout<<"class A1\n";}
};
class A2 : public A1
public :
A2()
{cout<<"class A2\n";}
};
class A3 : public A2
public :
A3()
{cout<<"class A3";}
};
int main()
A3 obj;
OUTPUT:
class A1
class A2
class A3
17
12. Develop a C++ program to swap private variable data of two objects
#include<iostream>
using namespace std;
class A{
int a;
public :
A(int i){a=i;}
void print(){
cout<<"a="<<a<<endl;
void swap(A &,A &);
};
void A :: swap(A &a1,A &a2)
int temp;
temp=a1.a;
a1.a=a2.a;
a2.a=temp;
int main(){
A obj1(2),obj2(3);
cout<<"Before swapping\n";
obj1.print();
obj2.print();
cout<<"After swapping\n";
swap(obj1,obj2);
obj1.print();
obj2.print();
}
18
OUTPUT:
Before swapping
a=2
a=3
After swapping
a=3
a=2
19
13. Develop a C++ program to declare a class. Declare pointer to class. Initialize
and display the contents of the class member.
#include<iostream>
using namespace std;
class A{
int l,b,h;
public:
void getdata(int x,int y,int z)
l=x;b=y;h=z;
void volume()
cout<<l*b*h;
};
int main()
A o;
o.getdata(3,4,5);
A *ptr;
ptr=&o;
cout<<"3*4*5=";
ptr->volume();
OUTPUT: 3*4*5=60
20
14. Develop a C++ program to raise an exception if the user given string is not
‘CSE’
#include<iostream>
using namespace std;
int main()
string s;
cout<<"Enter the text:";
cin>>s;
try
if(s!="CSE")
throw s;
cout<<"The given text is CSE";
catch(string s){
cout<<"Exception caught\n";
cout<<"Error!The given text is:"<<s<<endl;
return 0;
OUTPUT:
Enter the text:hi
Exception caught
Error!The given text is:hi
15. Develop a C++ program to raise an exception when user tries to draw below
minimum balance (Assume minimum balance as 1000 rupees)
21
#include<iostream>
using namespace std;
int main()
int s;
cout<<"Enter the amount to be drawn";
cin>>s;
try{
if(s<1000){
throw s;
cout<<"The Amount can be drawn"<<endl;
catch(int s){
cout<<"The Drawn amount is less than 1000"<<endl;
OUTPUT:
Enter the amount to be drawn500
The Drawn amount is less than 1000
22
16. Develop a C++ program to implement concept of pointer to function
#include<iostream>
using namespace std;
void product(int a,int b)
cout<<"The product is "<<a*b;
int main()
void (*ptr)(int,int)=&product;
(*ptr)(10,20);
OUTPUT:
The product is 200
23
17. Develop a C++ program to input list of candidates and find winner of the
election based on the received votes
#include<iostream>
using namespace std;
class elections{
int votes ;
string candidate;
public :
void get();
void winner(int n,elections obj[20]);
};
void elections :: get()
cout<<"enter the candidate name\n";
cin>>candidate;
cout<<"enter the number of votes\n";
cin>>votes;
void elections :: winner(int n,elections obj[20])
elections a;
a=obj[0];
int i;
for(i=0;i<n;i++)
if(obj[i].votes>a.votes)
a=obj[i];
cout<<"The winner is "<<a.candidate;
int main()
24
int i,n;
elections obj[20];
cout<<"enter the no of candidates\n";
cin>>n;
for(i=0;i<n;i++)
obj[i].get();
obj[0].winner(n,obj);
OUTPUT:
enter the no of candidates
enter the candidate name
nan
enter the number of votes
56
enter the candidate name
man
enter the number of votes
33
enter the candidate name
fan
enter the number of votes
78
The winner is fan
25
18. Develop a C++ program to implement hierarchical inheritance
#include<iostream>
using namespace std;
class A
public :
A()
cout<<"class A\n";
};
class B : public A
public :
B()
cout<<"class B\n";
};
class C : public A
public :
C()
cout<<"class C\n";
};
int main()
{
26
C a;
B b;
OUTPUT:
class A
class C
class A
class B
27
19. Develop a C++ program to use scope resolution operator. Display the various
values of the same variables declared at different scope levels.
#include<iostream>
using namespace std;
int a=10;
int main()
cout<<"a="<<a<<endl;
int a=20;
cout<<"inside main a="<<a<<endl;
a=30;
cout<<"Inside block a="<<a<<endl;
cout<<"inside block using :: a="<<::a<<endl;
cout<<"outside block using :: a="<<::a<<endl;
OUTPUT:
a=10
inside main a=20
Inside block a=30
inside block using :: a=10
outside block using :: a=10
28
20. Develop a C++ program to illustrate the functionality of multiple catch blocks
in exception handling
#include<iostream>
using namespace std;
void test(int x)
try
if (x==1)
throw x;
else if (x==0)
throw 1.9;
else if (x==2)
throw 2.1f;
else
throw "happy";
catch(int i)
cout << "caught integer"<<endl;
catch(double f)
cout << "caught double"<<endl;
catch(float f)
cout << "caught float"<< endl;
catch(...)
29
cout << "caught ellipsis"<< endl;
cout<<"end of try catch"<<endl;
int main()
test(3);
OUTPUT:
caught ellipsis
end of try catch
30
21. Write a C++ program to create an array of pointers. Invoke functions using
array of objects.
#include<iostream>
using namespace std;
class student
string name;
int rollno;
public:
void biodata();
void display();
};
void student :: biodata()
cout<<"Enter the name\n";
cin>>name;
cout<<"Enter the rollno\n";
cin>>rollno;
void student ::display()
cout<<"Name="<<name<<endl;
cout<<"Rollno="<<rollno<<endl;
int main(){
student * ptr = new student[4];
for(int i=0;i<4;i++)
(ptr+i)->biodata();
for(int i=0;i<4;i++)
31
(ptr+i)->display();
}}
OUTPUT:
Enter the name
nan
Enter the rollno
Enter the name
nann
Enter the rollno
Enter the name
nannn
Enter the rollno
Enter the name
nannnn
Enter the rollno
Name=nan
Rollno=1
Name=nann
Rollno=2
Name=nannn
Rollno=3
Name=nannnn
Rollno=4
32
22. Develop a C++ program to illustrate the concept of inner class
#include<iostream>
using namespace std;
class A{
public :
A()
cout<<"in class A "<<endl;
obj2.print();
class B
int a;
public :
void print()
cout<<"in class B";
}};
B obj2;
};
int main()
A::B obj;
A obj1;
OUTPUT:
in class A
in class B
33
23. Write a C++ program to use pointer for both base and derived classes and call
the member function. Use Virtual keyword.
#include<iostream>
using namespace std;
class B
public:
void disp(){cout<<"base display\n";}
virtual void show(){cout <<"base show\n";}
};
class D : public B
public:
void disp(){cout<<"derived display\n";}
void show(){cout <<"derived show\n";}
};
int main()
B b;
D d;
B *bptr;
cout << "bptr points to base\n";
bptr = &b;
bptr->disp(); //calls base version
bptr->show(); //calls base version
cout << "bptr points to derived\n";
bptr = &d;
bptr->disp(); //calls base version
bptr->show(); //calls derived version
return 0;
}
34
OUTPUT:
bptr points to base
base display
base show
bptr points to derived
base display
derived show
35
24. Develop a C++ program to illustrate the concept of default arguments
#include<iostream>
using namespace std;
void product(int a,int b,int c=10)
int mul;
mul=a*b*c;
cout<<"The product of three numbers is "<<mul<<endl;
int main()
product(5,4);
product(5,4,2);
OUTPUT:
The product of three numbers is 200
The product of three numbers is 40
36
25. Develop a C++ program to count the number of vowels in a given text file
#include<iostream>
using namespace std;
#include <iostream>
#include <fstream>
#include<conio.h>
using namespace std;
int main()
fstream f;
char ch;
int count=0;
f.open("num.txt");
if (!f)
cout<<"sorry";
else
while(!f.eof())
f.get(ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
count++;
cout<<"number of vowels "<<count;
f.close();
return 0;
}
37
26. Develop a C++ program to display number of characters, words, lines in a
given text file
#include <iostream>
#include <fstream>
using namespace std;
int main()
ofstream op;
op.open("data.txt");
op<<"hello students\n how are you \n what are you doing";
op.close();
int countChar=0;
char ch;
int countWord=0;
char w[30];
int countLine=0;
char lines[80];
ifstream input1,input2,input3;
input1.open("data.txt");
if(!input1)
cout<<"sorry";
else
while(!input1.eof())
input1.get(ch);
countChar++;
input2.open("data.txt");
if(!input2)
cout<<"sorry";
else{
while(!input2.eof())
{
38
input2>>w;
countWord++;
input3.open("data.txt");
if(!input3)
cout<<"sorry";
while(!input3.eof())
input3.getline(lines,80);;
countLine++;
cout<< "No. of characters in a file is \n"<<countChar<<endl;
cout<< "No. of words in a file is \n"<<countWord<<endl;
cout<< "No. of lines in a file is \n"<<countLine<<endl;
input1.close();
input2.close();
input3.close();
return 0;
27. Develop a C++ program to create a hole in a file
39
#include <iostream>
#include <fstream>
#include<conio.h>
using namespace std;
int main()
ofstream f;
f.open("nu.txt");
if (!f)
cout<<"sorry";
else
f<<"students";
int pos=f.tellp();
cout<<pos;
f.seekp(pos+10);
f<<"hai hello";
f.close();
return 0;
}
40
28. Develop a C++ program to write a student objects (3) to a file, read the same
from file and display the student details on screen (Note: use write() and
read() functions)
#include <iostream>
#include <fstream>
#include<conio.h>
using namespace std;
class student
int rno;
char name[30];
float marks;
public:
void getdata()
cout << "enter rno : ";
cin>>rno;
cin.ignore();
cout << "enter name : ";
cin.getline(name,30);
cout << "enter marks : ";
cin>>marks;
void dispdata()
cout<<"Name : "<<name<<endl;
cout<<"Roll No : "<<rno<<endl;
cout<<"Marks : "<<marks<<endl;
};
int main()
student s[3];
fstream f;
int i;
41
f.open("stud.txt",ios::out);
cout<<"Writing...."<<endl;
for(i=0;i<3;i++)
s[i].getdata();
f.write((char *)&s[i],sizeof(s[i]));
f.close();
f.open("stud.txt",ios::in);
cout<<"Reading ......."<<endl;
for(i=0;i<3;i++)
f.read((char *)&s[i],sizeof(s[i]));
s[i].dispdata();
f.close();
return 0;
}
42
29. Develop a C++ program to display the size of a given file
#include <iostream>
#include <fstream>
#include<conio.h>
using namespace std;
int main()
ifstream f;
int begin,end;
f.open("nu.txt");
if (!f)
cout<<"sorry";
else
begin=f.tellg();
f.seekg(0,ios::end);
end=f.tellg();
f.close();
cout <<"size of the file is "<<(end-begin)<< "bytes";
return 0;
}
43
30. Develop a C++ program to count the number of occurrences of a given word
in a given text file
#include<iostream>
using namespace std;
OUTPUT:
#include <iostream>
#include <fstream>
#include<conio.h>
#include<cstring>
using namespace std;
int main()
ifstream f;
char ch[30];
int count=0;
f.open("nu.txt");
if (!f)
cout<<"sorry";
else
while(!f.eof())
f>>ch;
if(strcmp(ch,"the")==0)
count++;
cout<<"number of words "<<count+1;
f.close();
return 0;
}
44
31. Develop a C++ program to concatenate the content of two given files and
write into third file
#include <iostream>
#include <fstream>
using namespace std;
int main()
ofstream op;
op.open("data1.txt");
op<<"hello students\nhow are you";
op.close();
op.open("data2.txt");
op<<"\nwhat are you doing\n ";
op.close();
char ch;
ifstream input;
ofstream output;
input.open("data1.txt");
output.open("sample.txt");
if(!input)
cout<<"sorry";
else
while(!input.eof())
input.get(ch);
output<<ch;
input.close();
input.open("data2.txt");
while(!input.eof())
input.get(ch);
45
output<<ch;;
input.close();
output.close();
return 0;
}
46
32. Develop a C++ program which read line of input from user and remove all
characters except alphabets
#include<iostream>
using namespace std;
int main()
char line[100],string[100];
int j=0;
cout<<"Enter a string\n";
cin.getline(line,100);
for(int i=0;line[i]!='\0';++i)
if((line[i]>='a'&&line[i]<='z')||(line[i]>='A'&&line[i]<='Z'))
string[j++]=line[i];
string[j]='\0';
cout<<"output string\n"<<string;
return 0;