OOPs in C++ Programming Manual
OOPs in C++ Programming Manual
BY
Mr. [Link]
ASSISTANT PROFESSOR([Link])
DEPARTMENT OF INFORMATION TECHNOLOGY
SRI RAMAKRISHNA ENGINEERING COLLEGE
COIMBATORE
EMAIL: [Link]@[Link]
1. Write a C++ program to find the largest of three numbers using inline function.
#include<iostream.h>
#include<conio.h>
inline int largest(int &a,int &b,int &c)
{
int
big=0;
if(a>b)
big=a;
else
big=b;
if(c>big)
big=c;
return big;
}
void main()
{
int a,b,c;
clrscr();
cout<<"Enter Three Numbers To Find The Largest "<<endl;
cout<<"a = ";
cin>>a;
cout<<"\nb = ";
cin>>b;
cout<<"\nc = ";
cin>>c;
int large=largest(a,b,c);
cout<<"\n Largest of "<<a<<","<<b<<" and "<<c<<" is "<<large;
getch();
}
********************************OUTOUT**********************************
#include<iostream.h>
#include<conio.h>
void exchange(int (&a)[],int &n);
void main()
{
int a[10],size;
clrscr();
cout<<"Enter the Array size : ";
cin>>size;
cout<<"Enter the Array elements :\n";
for(int i=0;i<size;i++)
cin>>a[i];
exchange(a,size);
cout<<"After sorting :\n";
for(i=0;i<size;i++)
cout<<a[i]<<endl;
getch();
}
*********************************OUTPUT********************************
Enter the Array size : 10
Enter the Array elements :
15 46 89 62 -5 -78 0 5 45
9
After sorting :
-78 -5 0 5 9
15 45 46 62
89
#include<iostream.h>
#include<conio.h>
#include<math.h>
int power(int a,int b);
double power(int i, double d);
void main()
{
int i1,i2,i3;
double d1;
clrscr();
cout<<"\nEnter Two Integer Numbers To Calculate Power : ";
cin >>i1>>i2;
int p1=power(i1,i2);
cout<<"\nThe Power of "<<i1<<" and "<<i2<<" is : "<<p1;
cout<<"\n\nEnter One Integer And One Double To calculate power :";
cin>>i3>>d1;
double p2=power(i3,d1);
cout<<"\nThe Power of "<<i3<<" and "<<d1<<" is : "<<p2;
getch();
}
********************************OUTPUT**********************************
#include<iostream.h>
#include<conio.h>
class distance
{
int feet,inch;
public:
void getdistance();
void putdistance();
void adddistance(distance d1,distance d2);
};
void distance::getdistance()
{
cout<<"\nEnter the feet : ";
cin>>feet;
cout<<"\nEnter the inches :";
cin>>inch;
}
void distance::putdistance()
{
cout<<"\n\nfeet = "<<feet;
cout<<"\tinch = "<< inch;
}
void distance::adddistance(distance d1,distance d2)
{
inch=[Link]+[Link];
feet=inch/12;
inch=inch%12;
feet=feet+[Link]+[Link];
}
void main()
{
distance d1,d2,d3;
clrscr();
cout<< "\nDistance 1 \n";
[Link]();
cout<< "\nDistance 2 \n";
[Link]();
[Link](d1,d2);
*********************************OUTPUT****************************
Distance 1
Enter the feet : 13
Enter the inches :11
Distance 2
Enter the feet : 11
Enter the inches :11
feet = 25 inch = 10
*************************OUTPUT*********************************
Enter employee details:
employee 1
NAME :ashok
CODE :111
employee 2
NAME :Annapurna
CODE :112
employee 3
NAME :anupama
CODE :113
employee 4
NAME :anuradha
CODE :114
employee 5
NAME :ashraya
CODE :115
employee 6
NAME :akash
CODE :116
Employee details are as follows :
NAME CODE
----------------------------------------------
Ashok 111
annapurna 112
anupama 113
anuradha 114
ashraya 115
Akash 116
void main()
{
clrscr();
Time t1(10,48,30);
Time t2(2,22,35);
Time t3; [Link](t1,t2);
cout<<"\nTime 1 is :";
[Link]();
cout<<"\nTime 2 is :";
***********************OUTPUT******************************
Time 1 is :10:48:30
Time 2 is :2:22:35
7. Create a class 'COMPLEX' to hold a complex number. Write a friend function to add
two complex numbers. Write a main function to add two COMPLEX objects.
#include<iostream.h>
#include<conio.h>
class complex
{
float real,imag;
public: void get_complex();
void show_complex();
friend complex add_complex(complex c1,complex c2);
};
void complex::get_complex()
{
cout<<"Enter real number :";
cin>> real;
cout<<"Enter Imaginary number :";
cin>>imag;
}
void complex::show_complex()
{
cout<<real<<"+i"<<imag;
}
complex add_complex(complex c1,complex c2)
{
complex c;
[Link]=[Link]+[Link];
[Link]=[Link]+[Link];
return c;
}
void main()
{
clrscr();
complex c1,c2,c3;
c1.get_complex();
c2.get_complex();
c3=add_complex(c1,c2);
cout<<"\nComplex Number 1 = ";
c1.show_complex();
cout<<"\nComplex Number 2 = ";
c2.show_complex();
cout<<"\nSum of Complex Number 1 and 2 = ";
c3.show_complex();
getch();
}
*******************************OUTPUT*****************************
8. Create a 'MATRIX' class of size m X n. Overload the ‘+’ operator to add two
MATRIX objects. Write a main function to implement it.
#include<iostream.h>
#include<conio.h>
class mat
{
int m,n,a[20][20];
public:
mat(int x,int y);
void readmat();
mat operator +(mat);
void display();
};
mat :: mat(int x,int y)
{
m=x;n=y;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
a[i][j]=0;
}
}
void mat :: readmat()
{
cout<<"\nenter matrix elements\n";
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
}
mat mat:: operator +(mat obj)
{
mat temp(m,n);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
temp.a[i][j]=a[i][j]+obj.a[i][j];
}
return temp;
}
void mat:: display()
{
int i,j;
for(i=0;i<m;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<"\t"<<a[i][j]; }
}
[Link] AP([Link])/Information Technology/SREC Page 13
OOPs with C++ Programming
void main()
{
int m1,n1;
clrscr();
cout<<"\nEnter the size(m,n) of matrix: ";
cin>>m1>>n1;
mat a(m1,n1),b(m1,n1),c(m1,n1);
cout<<"\nEnter martix 1: ";
[Link]();
cout<<"\nEnter matrix 2: ";
[Link]();
c=[Link] +(b);
cout<<"\nFirst Matrix :\n";
[Link]();
cout<<"\nSecond Matrix :\n";
[Link]();
cout<<"\nmatrix 1+matrix 2: ";
[Link]();
getch();
}
********************************OUTPUT**********************************
First Matrix :
3 3
3 3
Second Matrix :
4 4
4 4
matrix 1 + matrix 2:
7 7
7 7
*************************OUTPUT********************************
enter the order of matrix: 2 2
enter the matrix 1:
enter the matrix element2 2 2 2
enter the matrix 2:
enter the matrix element2 2 2 2
matrix 1:
2222
matrix 2:
2222
sum of two
matrix: 8 8 8 8
Rollnumber: 11111
Sub1=67
Sub2=56
Total= 123
class n
{
protected : int n;
public:
void getn()
{
cout<<"\nEnter the value for n : ";
cin>>n;
}
};
class p : public m , public n
{
public:
void display()
{
cout<<"\nM="<<m;
cout<<"\nN="<<n;
cout<<"\nM*N="<<m*n;
}
};
int main()
{
clrscr();
p p1;
[Link]();
[Link]();
[Link]();
getch();
return 0;
}
************************************OUTPUT******************************
Enter the value for m : 12
Enter the value for n : 10
M=12
N=10
M*N=120
11. Create a 'STRING' class which overloads ‘ = = ' operator to compare two STRING
objects.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *p;
int len;
public:
string(){ }
string( char *s)
{
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
friend int operator = =( string &s,string &t);
};
void main()
{
string s1,s2;
char str1[20],str2[20];
clrscr();
cout<<"\nEnter the first string : ";
cin>>str1;
cout<<"\nEnter the second string : ";
cin>>str2;
s1=str1;
s2=str2;
if(s1= =s2)
cout<<"\nStrings are equal";
else
cout<<"\nStrings are not equal";
getch();
}
*******************************OUTPUT 1********************************
Enter the first string : cpc
******************************OUTPUT 2**********************************
Enter the first string : CPC
12. Write a C++ program to illustrate ‘this’ pointer and pointers to derived classes.
#include<iostream.h>
#include<conio.h>
class BC
{
public:
int b;
void show()
{
cout<<"b= "<<b<<endl;
}
BC findlarge(BC obj)
{
if(b>obj.b)
return *this;
else
return obj;
}
};
class DC:public BC
{
public
: int d;
void show()
{
cout<<"b= "<<b<<endl;
cout<<"d= "<<d<<endl;
}
};
int main()
{
clrscr();
BC
b1,b2;
b1.b=10;
b2.b=20;
BC
Large=[Link](b2);
cout<<"\n Largest is :";
[Link]();
BC *bptr;
BC base;
bptr=&base;
bptr->b=100;
cout<<"Base pointer to base class\n";
bptr->show();
DC derived;
bptr=&derived;
bptr->b=200;
cout<<"Base pointer to base class\n";
bptr->show();
DC *dptr;
dptr=&derived;
dptr->d=300;
cout<<"Derived pointer to derived class\n";
dptr->show();
((DC*)bptr)->d=400;
cout<<"Type conversion\n";
((DC*)bptr)->show();
getch();
return 0;
}
**********************OUTPUT********************************
Largest is :b= 20
Base pointer to base
class b= 100
Base pointer to base
class b= 200
Derived pointer to derived
class b= 200
d= 300
Type
conversion b=
200
d= 400
#include<iostream.h>
#include<conio.h>
class shape
{
protected:double x, y;
public:void getdata(double a, double b)
{
x=a;
y=b;
}
virtual void display_area()=0;
};
class triangle:public shape
{
double triangle_area;
void display_area()
{
triangle_area=(1*x*y)/2;
cout<<"area of triangle is:"<<triangle_area<<endl;
}
};
class rectangle:public shape
{
double rectangle_area;
void display_area()
{
rectangle_area=x*y;
cout<<"area of rectangle is:"<<rectangle_area;
}
};
int main()
{
[Link] AP([Link])/Information Technology/SREC Page 27
clrscr();
shape *p;
OOPs with C++ Programming
triangle t;
rectangle r;
p=&t;
p ->getdata(10,30);
p->display_area();
p=&r;
p->getdata(20,30);
p->display_area();
getch();
return 0;
}
*******************************OUTPUT*******************************
area of triangle is:150
area of rectangle
is:600
14. Write a C++ program to read a list containing item name, item code and cost
interactively and display the data in a tabular format as shown below:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
int n,itcode[10];
char item[10][10];
float itcost[10];
clrscr();
cout<<"Enter the no of item:";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter the item details:"<<i+1;
cout<<"\nEnter name:"; cin>>item[i];
cout<”Enter code:";
cin>>itcode[i];
cout<<"Enter cost:";
cin>>itcost[i];
}
cout<<endl<<setw(15)<<"Item name"<<setw(15)<<"item code"<<setw(15)<<"cost"<<endl;
cout<<"\n------------------------------------- \n";
for(i=0;i<n;i++)
{
[Link]( ' - ' );
[Link](15);
cout<<item[i];
[Link](ios::left,ios::adjustfield);
[Link](15);
cout<<itcode[i];
[Link](ios::right,ios::adjustfield);
cout<<itcost[i]<<endl;
}
getch();
return 0;
}
***********************************OUTPUT*************************
15. Design your own manipulator to provide the following output specification for
printing money value:
1) 10 columns width
2) The character '$' at the beginning
3) Showing '+' sign.
4) Two digits precision
5) Filling of unused spaces with ' * '
6) Trailing zeros shown
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
ostream &moneyshow(ostream &output)
{
cout<<'$';
[Link]('*');
[Link](ios::showpos);
[Link](ios::showpoint);
[Link](10);
[Link](2);
return output;
}
void main()
{
double amount;
clrscr();
cout<<"enter the value:";
cin>>amount;
cout<<"\nyour money value:";
cout<<moneyshow<<amount;
getch();
[Link] AP([Link])/Information Technology/SREC Page 31
OOPs with C++ Programming
}
*************************OUTPUT 1***************************************
enter the value:12.2
*************************OUTPUT 2***************************************
16. Write a C++ program that uses a single file for both reading and writing the data.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
int main()
{
clrscr();
ofstream outf("[Link]");
cout<<"Enter the
filename:"; char name[30];
cin>>name;
outf<<name<<"\n";
cout<<"Enter ITEM cost:";
float cost;
cin>>cost;
outf<<cost;
[Link]();
ifstream inf("[Link]");
inf>>name;
inf>>cost;
cout<<"The name of the item is:"<<name;
cout<<"\nItem cost is :"<<cost;
[Link]();
getch();
return 0;
}
**************************************OUTPUT****************************
17. A file contains a list of names and telephone numbers in the following form:
Name Tel. No.
Write a C++ program to read the file and output the list in the tabular format. The name
should be left-justified and numbers right-justified. Use a class object to store each set of
data.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<fstream.h>
class phone
{
public:
char name[20];
long int phone_no;
void getdata()
{
cout<<"Enter the name and phone_no"<<"\n";
cin>>name>>phone_no;
}
void putdata()
{
cout<<endl<<setw(20)<<setiosflags(ios::left)<<name<<setw(10)
<<setiosflags(ios::right)<<phone_no<<endl;
}
};
void main()
{
fstream f;
phone ph;
int n;
clrscr();
cout<<"\nEnter the total no_of records:";
cin>>n;
for(int i=0;i<n;i++)
{
[Link]();
[Link]((char *)&ph,sizeof(ph));
}
[Link](0);
cout<<"\n\nThe content of the file is:\n\n";
cout<<setw(20)<<setiosflags(ios::left)<<"NAME"<< setw(10)
<< setiosflags(ios::right) <<"TELEPHONE_NO";
while([Link]((char *)&ph,sizeof(ph)))
[Link]();
[Link]();
getch();
}
**********************************OUTPUT********************************
Enter the total no_of records:3
Enter the name and phone_no
RAGHU
2456570
Enter the name and phone_no
RAJEEV
2457859
Enter the name and phone_no
RANJU
2451230
NAME TELEPHONE_NO
RAGHU 2456570
RAJEEV 2457859
18. Write an interactive, menu-driven program that will access the file created in
program No.17 and implement the following tasks:
i) To determine the telephone numbers of the specified person.
ii) To determine the name if a telephone number is given.
iii) To update the telephone number whenever there is a change.
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
class phone
{
public: long int no;
char name[10];
void getdata()
{
cout<<endl<<"enter the name :";
cin>>name;
cout<<endl<<"enter the telephone number :";
cin>>no;
}
void putdata()
{
cout<<endl<<setw(10)<<name<<endl<<setw(10)<<no<<endl;
}
};
int main()
{
phone ph;
fstream f;
clrscr();
int n;
[Link]("[Link]",ios::trunc|ios::in|ios::out|ios::ate);
cout<<"Enter the number of records :";
cin>>n;
for(int i=0;i<n;i++)
{
[Link]();
[Link]((char *)&ph,sizeof(ph));
}
[Link](0);
cout<<endl<<"conents of the file are as follows ";
while([Link]((char *)&ph,sizeof(ph)))
[Link]();
int choice;
while(1)
{
cout<<"\[Link] name from telephone number";
cout<<"\[Link] telephone number from name";
cout<<"\[Link] the telephone number";
cout<<"\[Link]";
cout<<"\nenter ur choice ";
cin>>choice;
[Link](0);
[Link]();
switch(choice)
{
case 1:cout<<"\nEnter the telephone number to get the name ";
int no;
cin>>no;
cout<<endl<<p;
[Link](p-loc);
[Link]((char *)&ph,sizeof(ph));
}
}
cout<<endl<<"After modification the content of the file is as follows ";
[Link]();
[Link](0);
while([Link]((char *)&ph,sizeof(ph)))
[Link]();
break;
case 4:exit(0);
}
}
}
******************************OUTPUT**********************************
Enter the number of records :3
enter the name :aaa
enter the telephone number
:11111 enter the name :bbb
enter the telephone number
:22222 enter the name :ccc
enter the telephone number
:33333 conents of the file are as
follows
aaa
11111
bbb
22222
ccc
3333
3
[Link] name from telephone
number [Link] telephone number
from name [Link] the telephone
number [Link]
enter ur choice 1
Enter the telephone number to get the name 11111
found
aaa
11111
not foundnot foundnot found [Link]
name from telephone number
[Link] telephone number from
name [Link] the telephone number
[Link]
enter ur choice 2
Enter the name to get the telephone numberccc
ccc
33333
[Link] name from telephone
number [Link] telephone number
from name [Link] the telephone
number [Link]
enter ur choice 3
Enter the name to modify the telephone number
ccc ccc
33333
Enter the new details to modify
: enter the name :ccc
enter the telephone number
:44444 42
[Link] AP([Link])/Information Technology/SREC Page 40
OOPs with C++ Programming
19. Write a C++ program that displays the size (in bytes) of a given file. The name of
the file is specified as command line argument.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main(int arge,char*argv[])
{
int bcount=0;
char cl;
ifstream testfile;
clrscr();
cout<<"enter the file name\n";
cin>>argv[1];
[Link](argv[1],ios::in);
while(testfile)
{
[Link]((char*)&cl,sizeof(cl));
++bcount;
}
[Link]();
cout<<"size of the given file:"<<argv[1]<<"is"<<bcount<<"bytes"<<endl;
getch();
}
*********************************OUTPUT********************************
20. Define a function template for finding the minimum value contained in an array. Write
main( ) function to find the minimum value of integer array and minimum value of floating
point numbers in an array.
#include<iostream.h>
#include<conio.h>
template<class T>
T minimum(T a[],int size)
{
T min=a[0];
for(int i=0;i<size;i++)
{
if(a[i]<min)
min=a[i];
}
return min;
}
int main()
{
int a[10],size,i,min1;
float b[10],min2;
clrscr();
cout<<"enter the size value:\n";
cin>>size;
cout<<"enter the integer aray elements\n";
for(i=0;i<size;i++)
cin>>a[i];
cout<<"enter the floating array elements\n";
for(i=0;i<size;i++)
cin>>b[i];
min1=minimum(a,size);
elements 20 30 10 38 28
21. Write a class template to represent a generic vector. Include member functions to
perform the following tasks:
1) To create the vector.
2) To modify the value of a given element.
3) To multiply the vector by a scalar value.
4) To display the vector in the form (10, 20, 30,…..)
#include<iostream.h>
#include<string.h>
#include<conio.h>
template<class T>
class vector
{
T *v;
int size;
public:
vector(int m);
void create(T *a);
void modify(int);
void multiply(int);
void display();
};
template<class T>
vector< T > :: vector(int m)
{
v=new T[size=m];
for(int i=0;i<size;i++)
v[i]=0;
}
template<class T>
void vector<T>::create(T*a)
{
for(int i=0;i<size;i++)
{
cin>>a[i];
v[i]=a[i];
}
}
template<class T>
void vector<T>::modify(int k)
{
v[k]=v[k]+10;
}
template<class T>
void vector<T>::display()
{
[Link](ios::showpoint);
cout<<"\n(";
for(int i=0;i<size;i++)
cout<<v[i]<<", ; cout<<")\n";
}
int main()
{
vector <float> v1(5);
vector <int> v2(5);
float *x;
int *y;
int i, s;
cout<<"Enter the float vector element :\n";
[Link](x);
cout<<"Enter the Interger vector element :\n";
[Link](y);
cout<<"enter the element u want to modify in float vector :";
cin>>i;
[Link](i);
[Link]();
cout<<"\nenter the element u want to modify in int vector :";
cin>>i;
[Link](i);
[Link]();
cout<<"\nenter the number to calculate the scalar product :";
cin>>s;
[Link](s);
[Link](s);
cout<<"\nthe Float vector after scalar product is as follows :";
[Link]();
cout<<"\nthe integer vector after scalar product is as follows :";
[Link]();
getch();
return(0);
}
***********************************OUTPUT*******************************