Oops Rec Final
Oops Rec Final
Oops Rec Final
1. Develop a C++ program to overload the addition operator (+) to add two
complex numbers
#include<iostream>
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
#include<iostream>
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;
};
{
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>
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;
return 0;
OUTPUT:
30
30
7
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
class A
public :
A(){cout<<"class A\n";}
};
class B
public :
B(){cout<<"class B\n";}
};
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>
class student{
string name;
char grade;
int rollno;
public:
void display()
cout<<rollno;
cout<<"\t"<<name;
cout<<"\t"<<grade;
cout<<endl;
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:
1 vikas A
2 shiva B
3 venu C
11
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
class company{
string empname;
int empno,basic,hra;
float it,net,gross,da;
public:
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;
cin>>n;
company E[n];
for(i=0;i<n;i++)
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:
return x+y;
return x+y+z;
return x+y;
int main()
cout<<"addition of 4,5="<<add(4,5)<<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
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>
class A{
int a;
public :
A(int i){a=i;}
void print(){
cout<<"a="<<a<<endl;
};
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>
class A{
int l,b,h;
public:
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>
int main()
string s;
cin>>s;
try
if(s!="CSE")
throw s;
catch(string s){
cout<<"Exception caught\n";
return 0;
OUTPUT:
Exception caught
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>
int main()
int s;
cin>>s;
try{
if(s<1000){
throw s;
catch(int s){
OUTPUT:
#include<iostream>
int main()
void (*ptr)(int,int)=&product;
(*ptr)(10,20);
OUTPUT:
17. Develop a C++ program to input list of candidates and find winner of the
election based on the received votes
#include<iostream>
class elections{
int votes ;
string candidate;
public :
void get();
};
cin>>candidate;
cin>>votes;
elections a;
a=obj[0];
int i;
for(i=0;i<n;i++)
if(obj[i].votes>a.votes)
a=obj[i];
int main()
24
int i,n;
elections obj[20];
cin>>n;
for(i=0;i<n;i++)
obj[i].get();
obj[0].winner(n,obj);
OUTPUT:
nan
56
man
33
fan
78
#include<iostream>
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>
int a=10;
int main()
cout<<"a="<<a<<endl;
int a=20;
a=30;
OUTPUT:
a=10
20. Develop a C++ program to illustrate the functionality of multiple catch blocks
in exception handling
#include<iostream>
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)
catch(double f)
catch(float f)
catch(...)
29
int main()
test(3);
OUTPUT:
caught ellipsis
21. Write a C++ program to create an array of pointers. Invoke functions using
array of objects.
#include<iostream>
class student
string name;
int rollno;
public:
void biodata();
void display();
};
cin>>name;
cin>>rollno;
cout<<"Name="<<name<<endl;
cout<<"Rollno="<<rollno<<endl;
int main(){
for(int i=0;i<4;i++)
(ptr+i)->biodata();
for(int i=0;i<4;i++)
31
(ptr+i)->display();
}}
OUTPUT:
nan
nann
nannn
nannnn
Name=nan
Rollno=1
Name=nann
Rollno=2
Name=nannn
Rollno=3
Name=nannnn
Rollno=4
32
#include<iostream>
class A{
public :
A()
obj2.print();
class B
int a;
public :
void print()
}};
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>
class B
public:
};
class D : public B
public:
};
int main()
B b;
D d;
B *bptr;
bptr = &b;
bptr = &d;
return 0;
}
34
OUTPUT:
base display
base show
base display
derived show
35
#include<iostream>
int mul;
mul=a*b*c;
int main()
product(5,4);
product(5,4,2);
OUTPUT:
25. Develop a C++ program to count the number of vowels in a given text file
#include<iostream>
#include <iostream>
#include <fstream>
#include<conio.h>
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++;
f.close();
return 0;
}
37
#include <iostream>
#include <fstream>
int main()
ofstream op;
op.open("data.txt");
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++;
input1.close();
input2.close();
input3.close();
return 0;
#include <iostream>
#include <fstream>
#include<conio.h>
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>
class student
int rno;
char name[30];
float marks;
public:
void getdata()
cin>>rno;
cin.ignore();
cin.getline(name,30);
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
#include <iostream>
#include <fstream>
#include<conio.h>
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();
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>
OUTPUT:
#include <iostream>
#include <fstream>
#include<conio.h>
#include<cstring>
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++;
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>
int main()
ofstream op;
op.open("data1.txt");
op.close();
op.open("data2.txt");
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>
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;