You are on page 1of 77

NotesHub

(noteshub.co.in)

OOPS LAB FILE


Th
4 Semester

To contribute, mail us at support@noteshub.co.in

[name]
[roll no]
EXPERIMENT – 1
Aim – Write a program to find factorial of a number using recursion
#include<iostream.h>
#include<conio.h>
long factorial(int n)
{
if(n==0)
return 1;
else
return (n*factorial(n-1));
}
void main()
{
clrscr();
int n;
cout<<"Enter number:";
cin>>n;
cout<<"Factorial of "<<n<<" is "<<factorial(n);
getch();
}

[name]
[roll no]
EXPERIMENT – 2
Aim – Write a program to find smallest of two numbers using macros
#include<iostream.h>
#include<conio.h>
#define smallest(a,b) ((a<b)?a:b)
void main()
{
clrscr();
int a,b;
cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;
cout<<"Smallest="<<smallest(a,b);
getch();
}

[name]
[roll no]
EXPERIMENT – 3
Aim – Write a program to implement manipulators
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
clrscr();
float d=2.0,e=3.0,f;
f=d/e;
cout<<"\nUSE OF setprecision==>\n";
cout<<setprecision(1)<<f<<endl;
cout<<setprecision(2)<<f<<endl;
cout<<setprecision(3)<<f<<endl;
int b=200,c=300;
cout<<"USE OF setw==>\n";
cout<<setw(5)<<b<<setw(5)<<c<<endl;
cout<<setw(7)<<b<<setw(7)<<c;
cout<<"\nUSE OF setfill==>\n";
cout<<setfill('*');
cout<<setw(5)<<b<<setw(5)<<c<<endl;
cout<<setw(7)<<b<<setw(7)<<c<<endl;
int a=10;
cout<<"USE OF endl==>\n";
cout<<"OOPS"<<endl<<"OOPS";
cout<<"\nUSE OF hex,decimal AND oct==>";
cout<<"\nhex:"<<hex<<a;
cout<<"\ndec:"<<dec<<a;
cout<<"\noct:"<<oct<<a;
cout<<"\nUSE OF setiosflags==>\n";
cout<<setiosflags(ios::showbase);
cout<<setiosflags(ios::dec);
cout<<"dec="<<a<<endl;
cout<<setiosflags(ios::hex);
cout<<"hex="<<a<<endl;
cout<<setiosflags(ios::oct);
cout<<"oct="<<a<<endl;
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 4
Aim – Write a program to find area of triangle, circle and rectangle
using the concept of function overloading
#include<iostream.h>
#include<conio.h>
#include<math.h>
void area(float a,float b,float c)
{
float s,area;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"AREA="<<area<<" square units";
}
void area(float r)
{
float area;
area=3.14*r*r;
cout<<"AREA="<<area<<" square units";
}
void area(float l,float b)
{
float area;
area=l*b;
cout<<"AREA="<<area<<" square units";
}
void main()
{
clrscr();
float a,b,c;
int ch;
cout<<"1.Area of Triangle"<<endl;
cout<<"2.Area of Circle"<<endl;
cout<<"3.Area of Rectangle"<<endl;
cout<<"Enter choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter first side:";
cin>>a;
cout<<"Enter second side:";

[name]
[roll no]
cin>>b;
cout<<"Enter third side:";
cin>>c;
area(a,b,c);
break;
case 2:cout<<"Enter radius:";
cin>>a;
area(a);
break;
case 3:cout<<"Enter length:";
cin>>a;
cout<<"Enter breadth:";
cin>>b;
area(a,b);
break;
default:cout<<"Wrong choice";
}
getch();
}

[name]
[roll no]
EXPERIMENT – 5
Aim – Write a program having a function power to raise a number m to
power n under following conditions:

(A) The function takes a double value of m and int value of n


(B) The function uses default value of n to calculate squares
when this argument is omitted
#include<iostream.h>
#include<conio.h>
void power(double m,int n=2)
{
double pw=1;
for(int i=1;i<=n;i++)
{
pw*=m;
}
cout<<m<<"^"<<n<<"="<<pw<<endl;
}
void main()
{
clrscr();
double m;
int n;
cout<<"Enter base:";
cin>>m;
cout<<"Enter power:";
cin>>n;
cout<<"Without using default argument==>"<<endl;
power(m,n);
cout<<"Using default argument==>"<<endl;
power(m);
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 6
Aim – Write a program to take name, address as character array, age
as int, salary as float and contains inline function to set the values and
display it.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class employee
{
private:
char name[50];
char address[20];
int age;
float salary;
public:
void input()
{
cout<<"Enter Name:";
gets(name);
cout<<"Enter Address:";
gets(address);
cout<<"Enter Age:";
cin>>age;
cout<<"Enter Salary:";
cin>>salary;
}
void output();
};
void employee::output()
{
cout<<"Name:";
puts(name);
cout<<"Address:";
puts(address);
cout<<"Age:"<<age<<endl;
cout<<"Salary:"<<salary<<endl;
}
void main()
{
clrscr();

[name]
[roll no]
employee e;
cout<<"Enter the details==>"<<endl;
e.input();
cout<<"\nDetails==>"<<endl;
e.output();
getch();
}

[name]
[roll no]
EXPERIMENT – 7
Aim – Write a program to create a class TIME with members hours,
minutes and seconds. Take input, add two objects by passing them to
function and display the result.
#include<iostream.h>
#include<conio.h>
class time
{
private:
int hr;
int min;
int sec;
public:
void input();
void output();
void add(time t1,time t2);
};
void time::input()
{
cout<<"Enter number of hours:";
cin>>hr;
cout<<"Enter number of minutes:";
cin>>min;
cout<<"Enter number of seconds:";
cin>>sec;
}
void time::output()
{
cout<<"Hours:"<<hr<<endl;
cout<<"Minutes:"<<min<<endl;
cout<<"Seconds:"<<sec<<endl;
}
void time::add(time t1,time t2)
{
sec=t1.sec+t2.sec;
min=0;
hr=0;
while(sec>60)
{
min+=1;

[name]
[roll no]
sec-=60;
}
min+=t1.min+t2.min;
while(min>60)
{
hr+=1;
min-=60;
}
hr+=t1.hr+t2.hr;
}
void main()
{
clrscr();
time t1,t2,t3;
cout<<"Enter T1==>"<<endl;
t1.input();
cout<<"\nEnter T2==>"<<endl;
t2.input();
t3.add(t1,t2);
cout<<"\nSum of T1 and T2==>"<<endl;
t3.output();
getch();
}

[name]
[roll no]
EXPERIMENT – 8
Aim – Write a program for multiplication of two matrices.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],mult[10][10],r1,c1,r2,c2,i,j,k;
cout<<"Enter number of rows of 1st matrix:";
cin>>r1;
cout<<"Enter number of columns of 1st matrix:";
cin>>c1;
cout<<"Enter number of rows of 2nd matrix:";
cin>>r2;
cout<<"Enter number of columns of second matrix:";
cin>>c2;
while(c1!=r2)
{
cout<<"Error! Column of 1st matrux not equal to rows of 2nd matrix"<<endl;
cout<<"Enter number of rows of 1st matrix:";
cin>>r1;
cout<<"Enter number of columns of 1st matrix:";
cin>>c1;
cout<<"Enter number of rows of 2nd matrix:";
cin>>r2;
cout<<"Enter number of columns of second matrix:";
cin>>c2;
}
cout<<"Enter elements of 1st matrix==>"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<"Enter number:";
cin>>a[i][j];
}
}
cout<<"Enter elements of 2nd matrix==>"<<endl;
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)

[name]
[roll no]
{
cout<<"Enter number:";
cin>>b[i][j];
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
mult[i][j]+=a[i][k]+b[k][j];
}
}
}
getch();
clrscr();
cout<<"1st Matrix==>"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
cout<<"2nd Matrix==>"<<endl;
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cout<<b[i][j]<<"\t";
}
cout<<endl;
}
cout<<"Product==>"<<endl;
for(i=0;i<r1;i++)

[name]
[roll no]
{
for(j=0;j<c2;j++)
{
cout<<mult[i][j]<<"\t";
}
cout<<endl;
}
getch();
}

EXPERIMENT – 9

[name]
[roll no]
Aim – Write a program to create a class student which has data
members as name, branch, roll no, age, sex and marks in five subjects.
Display the name and the percentage of the student who has more than
70 percent. Use array of objects.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
private:
char name[20];
char branch[10];
int rn;
int age;
char sex;
float marks[5];
public:
float percentage;
void percent();
void input();
void output();
};
void student::percent()
{
float sum=0;
for(int i=0;i<5;i++)
{
sum+=marks[i];
}
percentage=(sum/500)*100;
}
void student::input()
{
cout<<"Enter Name:";
gets(name);
cout<<"Enter Branch:";
gets(branch);
cout<<"Enter Roll No.:";
cin>>rn;
cout<<"Enter Age:";
cin>>age;

[name]
[roll no]
cout<<"Enter Sex:";
cin>>sex;
for(int i=0;i<5;i++)
{
cout<<"Enter marks in subject "<<i+1<<"(out of 100):";
cin>>marks[i];
}
}
void student::output()
{
cout<<"Name:";
puts(name);
cout<<"Percentage:"<<percentage<<endl;
}
void main()
{
clrscr();
student s[10];
int n;
cout<<"Enter number of records to be entered:";
cin>>n;
for(int i=0;i<n;i++)
{
s[i].input();
}
cout<<endl;
for(int j=0;j<n;j++)
{
s[j].percent();
if(s[j].percentage>70)
{
s[j].output();
}
}
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 10
Aim – Write a program to enter any number and find its factorial using
constructor
#include<iostream.h>
#include<conio.h>
class copy
{
private:
int var,fact;
public:
copy(int temp)
{
var = temp;
}
double calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact = fact * i;
}
return fact;
}
};
void main()
{
clrscr();
int n;
cout<<"Enter the Number : ";
cin>>n;
copy obj(n);
copy cpy=obj;
cout<<"Factorial of "<<n<<" is:"<<obj.calculate()<<endl;
cout<<"Factorial of "<<n<<" is:"<<cpy.calculate();
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 11
Aim – Write a program to perform addition of two complex numbers
using constructor overloading. The first constructor which takes no
argument is used to create objects which are not initialized, second
which takes one argument is used to initialize real and imaginary parts
to equal value and third which takes two arguments is used to initialize
real and imaginary part to two different values.
#include<iostream.h>
#include<conio.h>
class complex
{
private:
double real;
double imag;
public:
complex();
complex(double a);
complex(double x,double y);
void add(complex c1,complex c2);
void show();
};
complex::complex()
{
real=0.0;
imag=0.0;
}
complex::complex(double a)
{
real=a;
imag=a;
}
complex::complex(double x,double y)
{
real=x;
imag=y;
}
void complex::add(complex c1,complex c2)
{

[name]
[roll no]
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}
void complex::show()
{
if(imag>0)
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
else
{
cout<<real<<imag<<"i"<<endl;
}
}
void main()
{
clrscr();
complex c1(6,8);
complex c2(5);
complex c3;
c3.add(c1,c2);
cout<<"First complex number==>"<<endl;
c1.show();
cout<<"Second complex number==>"<<endl;
c2.show();
cout<<"Sum==>"<<endl;
c3.show();
getch();
}

[name]
[roll no]
EXPERIMENT – 12
Aim – Write a program to generate a Fibonacci series using copy
constructor.
#include<iostream.h>
#include<conio.h>
class fibonacci
{
private:
long int f0,f1,fib;
public:
fibonacci()
{
f0=0;
f1=1;
fib=f0+f1;
}
fibonacci(fibonacci&ptr)
{
f0=ptr.f0;
f1=ptr.f1;
fib=ptr.fib;
}
void increment()
{
f0=f1;
f1=fib;
fib=f0+f1;
}
void display()
{
cout<<fib<<'\t';
}
};
void main()
{
clrscr();
int n;
fibonacci number;
cout<<"Enter number of elements:";
cin>>n;

[name]
[roll no]
for(int i=1;i<=n;i++)
{
number.display();
number.increment();
}
getch();
}

[name]
[roll no]
EXPERIMENT – 13
Aim – Write a program to find biggest of three numbers using friend
function.
#include<iostream.h>
#include<conio.h>
class largest
{
private:
int x,y,z;
public:
void num()
{
cout<<"Enter the first number:";
cin>>x;
cout<<"Enter the second number:";
cin>>y;
cout<<"Enter the third number:";
cin>>z;
}
friend void max(largest l);
};
void max(largest l)
{
if((l.x>l.y)&&(l.x>l.z))
{
cout<<"Largest="<<l.x<<endl;
}
else if((l.y>l.x)&&(l.y>l.z))
{
cout<<"Largest="<<l.y<<endl;
}
else
{
cout<<"Largest="<<l.z<<endl;
}
}
void main()
{
clrscr();
largest l;

[name]
[roll no]
l.num();
max(l);
getch();
}

EXPERIMENT – 14
[name]
[roll no]
Aim – Write a program to demonstrate the use of friend function with
inline assignment.
#include<iostream.h>
#include<conio.h>
class Humidity;
class Temperature
{
private:
int m_temp;
public:
Temperature(int temp=0)
{
m_temp=temp;
}
void setTemperature(int temp)
{
m_temp=temp;
}
friend void printWeather(Temperature &temperature,Humidity&humidity);
};
class Humidity
{
private:
int m_humidity;
public:
Humidity(int humidity=0)
{
m_humidity=humidity;
}
void setHumidity(int humidity)
{
m_humidity=humidity;
}
friend void printWeather(Temperature &temperature,Humidity&humidity)
{
cout<<"The temperature is "<<temperature.m_temp<<" and the humidity is
"<<humidity.m_humidity<<endl;
}
};
void main()
{
clrscr();

[name]
[roll no]
Humidity hum(10);
Temperature temp(12);
printWeather(temp, hum);
getch();
}

EXPERIMENT – 15

[name]
[roll no]
Aim – Write a program to find the greatest of two given numbers in two
different classes using friend function.
#include<iostream.h>
#include<conio.h>
class B;
class A
{
private:
int x;
public:
void num()
{
cout<<"Enter number:";
cin>>x;
}
friend void max(A a,B b);
};
class B
{
private:
int x;
public:
void num()
{
cout<<"Enter number:";
cin>>x;
}
friend void max(A a,B b);
};
void max(A a,B b)
{
if(a.x>b.x)
{
cout<<"Largest:"<<a.x<<endl;
}
else
{
cout<<"Largest:"<<b.x<<endl;
}
}
void main()
{

[name]
[roll no]
clrscr();
A a;
B b;
a.num();
b.num();
max(a,b);
getch();
}

EXPERIMENT – 16

[name]
[roll no]
Aim – Write a program to find the sum of two numbers declared in a
class and display the numbers and sum using friend function.
#include <iostream.h>
#include <conio.h>
class A
{
private:
int x,y;
public:
void num()
{
cout<<"Enter the first number:";
cin>>x;
cout<<"Enter the second number:";
cin>>y;
}
friend void sum(A a);
};
void sum(A a)
{
cout<<"First number:"<<a.x<<endl;
cout<<"Second number:"<<a.y<<endl;
cout<<"Their sum:"<<a.x+a.y<<endl;
}
void main()
{
clrscr();
A a;
a.num();
sum(a);
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 17
Aim – Write a program create a base class basic_info with data
members name, roll no, sex and member functions getdata and display.
Derive a class physical_fit from basic_info which has data members as
height and weight and member functions getdata and display. Display
all the information using object of the derived class.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class basic_info
{
private:
char name[25];
int rno;
char sex;
public:
void getdata();
void putdata();
};
void basic_info::getdata()
{
cout<<"Enter name:";
gets(name);
cout<<"Enter rollno:";
cin>>rno;
cout<<"Enter sex:";
cin>>sex;
}
void basic_info::putdata()
{
cout<<endl<<endl<<"Name :";
puts(name);
cout<<"Roll No. :"<<rno<<endl;
cout<<"Sex :"<<sex<<endl;
}
class phy_fit:publicbasic_info
{
private:
float ht;

[name]
[roll no]
float wt;
public:
void input()
{
getdata();
cout<<"Enter height(in cms):";
cin>>ht;
cout<<"Enter weight(in kg):";
cin>>wt;
}
void display()
{
putdata();
cout<<"Height :"<<ht<<endl;
cout<<"Weight :"<<wt<<endl;
}
};
void main()
{
clrscr();
phy_fitobj;
obj.input();
obj.display();
getch();
}

EXPERIMENT – 18
[name]
[roll no]
Aim – Write a program to create a class first with data members book
no, book name, and member function getdata and putdata. Create a
class second with data members author name, publisher and member
functions getdata and showdata. Derive a third class from first and
second with data members no of pages and year of publication. Display
all these information using array of objects of third class.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class first
{
private:
int bno;
char bname[50];
public:
void input()
{
cout<<"Enter Book No.:";
cin>>bno;
cout<<"Enter Book name:";
gets(bname);
}
void output()
{
cout<<endl<<endl<<"Book No. :"<<bno<<endl;
cout<<"Book Name :";
puts(bname);
}
};
class second
{
private:
char author[25];
char publisher[25];
public:
void indata()
{
cout<<"Enter Author:";
gets(author);
cout<<"Enter Publisher:";

[name]
[roll no]
gets(publisher);
}
void outdata()
{
cout<<"Author :";
puts(author);
cout<<"Publisher :";
puts(publisher);
}
};
class third:publicfirst,public second
{
private:
int pgn;
int yr;
public:
void in()
{
input();
indata();
cout<<"Enter number of pages:";
cin>>pgn;
cout<<"Enter Release year:";
cin>>yr;
}
void out()
{
output();
outdata();
cout<<"Number of pages: "<<pgn<<endl;
cout<<"Release Year : "<<yr;
}
};
void main()
{
clrscr();
third t[5];
int i,n;
cout<<"Enter number of enteries to be done:";
cin>>n;
for(i=0;i<n;i++)
{
t[i].in();
}

[name]
[roll no]
cout<<"Press anu key to continue....";
getch();
clrscr();
for(i=0;i<n;i++)
{
cout<<endl<<endl<<"DETAIL NO. "<<i+1<<"==>"<<endl;
t[i].out();
}
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 19
Aim – Write a program to design three classes student, exam and result.
The student class has data members name, roll no. The exam class
inherits student class and has marks scored in six subjects as data
members. Derive the class result from exam class which has total marks
as data member.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
private:
char name[25];
int rno;
public:
void input()
{
cout<<"Enter name:";
gets(name);
cout<<"Enter roll no:";
cin>>rno;
}
void output()
{
cout<<endl<<endl<<"NAME :";
puts(name);
cout<<"ROLL NO. :"<<rno<<endl;
}
};
class exam: public student
{
private:
float mks[6];
public:
void indata();
float add();
};
void exam::indata()
{

[name]
[roll no]
int i;
input();
cout<<"Enter marks in six subjects (out of 100)==>"<<endl;
for(i=0;i<6;i++)
{
cout<<"Enter marks in subject "<<i+1<<":";
cin>>mks[i];
}
}
float exam::add()
{
int i=0;
float sum=0;
for(i=0;i<6;i++)
{
sum=sum+mks[i];
}
return sum;
}
class result: public exam
{
private:
float tm;
public:
void display()
{
tm=add();
output();
cout<<"Total Marks:"<<tm<<"/600"<<endl;
}
};
void main()
{
clrscr();
result r;
r.indata();
r.display();
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 20
Aim – Write a program to create a base class called shape. Use this
class to store two double type values. Derive two classes called triangle
and rectangle from the base class. Add to the base class, a member
function getdata to initialize base class data members and another
member function to compute and display the area of the figures. Make
display a virtual function and redefine this function in derived class to
suit their requirements.
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
double l,b;
public:
void getdata(double l1,double b1)
{
l=l1;
b=b1;
}
virtual void display_area()=0;
};
class triangle:public shape
{
public:
void display_area()
{
double area;
area=0.5*l*b;
cout<<"Area of triangle="<<area<<" sq units";
}
};
class rectangle:public shape
{
public:
void display_area()
{
double area;
[name]
[roll no]
area=l*b;
cout<<"Area of rectangle="<<area<<" sq units";
}
};
void main()
{
clrscr();
double a,b;
shape *s1,*s2;
triangle t;
s1=&t;
cout<<"TRIANGLE==>"<<endl;
cout<<"Enter length of base of the triangle:";
cin>>a;
cout<<"Enter height of the triangle:";
cin>>b;
s1->getdata(a,b);
s1->display_area();
rectangle r;
s2=&r;
cout<<endl<<endl<<"RECTANGLE==>"<<endl;
cout<<"Enter length:";
cin>>a;
cout<<"Enter breadth:";
cin>>b;
s2->getdata(a,b);
s2->display_area();
getch();
}

EXPERIMENT – 21
[name]
[roll no]
Aim – Write a program to overload unary increment(++) and
decrement(--) operators
#include<iostream.h>
#include<conio.h>
class overloading
{
private:
int a,b,c,d;
public:
void input()
{
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
cout<<"Enter the value of d:";
cin>>d;
}
void output()
{
cout<<"Value of a:"<<a<<endl;
cout<<"Value of b:"<<b<<endl;
cout<<"Value of c:"<<c<<endl;
cout<<"Value of d:"<<d<<endl;
}
void operator ++()
{
a++;
b++;
cout<<"Incremented value of a:"<<a<<endl;
cout<<"Incremented value of b:"<<b<<endl;
}
void operator --()
{
c--;
d--;
cout<<"Decremented value of c:"<<c<<endl;
cout<<"Decremented value of d:"<<d<<endl;
}
};

[name]
[roll no]
void main()
{
clrscr();
overloading o;
o.input();
o.output();
o++;
o--;
getch();
}

EXPERIMENT – 22
Aim – Write a program to overload binary +, -, *, / and % operators
[name]
[roll no]
#include<iostream.h>
#include<conio.h>
class overloading
{
private:
int a;
public:
void input()
{
cout<<"Enter a number:";
cin>>a;
}
void output()
{
cout<<"The number is:"<<a<<endl;
}
overloading operator +(overloading o)
{
overloading o1;
o1.a=a+o.a;
return o1;
}
overloading operator -(overloading o)
{
overloading o1;
o1.a=a-o.a;
return o1;
}
overloading operator *(overloading o)
{
overloading o1;
o1.a=a*o.a;
return o1;
}
overloading operator /(overloading o)
{
overloading o1;
o1.a=a/o.a;
return o1;
}
overloading operator %(overloading o)
{
overloading o1;
o1.a=a%o.a;

[name]
[roll no]
return o1;
}
};
void main()
{
clrscr();
overloading o2,o3,o4,o5,o6,o7,o8;
o2.input();
o3.input();
o2.output();
o3.output();
o4=o2+o3;
cout<<"SUM"<<endl;
o4.output();
o5=o2-o3;
cout<<"DIFFERENCE"<<endl;
o5.output();
o6=o2*o3;
cout<<"PRODUCT"<<endl;
o6.output();
o7=o3/o2;
cout<<"DIVISION"<<endl;
o7.output();
o8=o3%o2;
cout<<"REMAINDER"<<endl;
o8.output();
getch();
}

[name]
[roll no]
EXPERIMENT – 23
Aim – Write a program to overload relational operators
#include<iostream.h>
#include<conio.h>
enumbool {false,true};
class distance
{
private:
int feet;
int inches;
public:
distance()
{

[name]
[roll no]
feet=0;
inches=0;
}
distance(int f,int i)
{
feet=f;
inches=i;
}
void show()
{
cout<<"F:"<<feet<<"\tI:"<<inches<<endl;
}
bool operator <(const distance &d)
{
if(feet<d.feet)
{
return true;
}
if((feet==d.feet)&&(inches<d.inches))
{
return true;
}
return false;
}
bool operator >(const distance &d)
{
if(feet>d.feet)
{
return true;
}
if((feet==d.feet)&&(inches>d.inches))
{
return true;
}
return false;
}
bool operator <=(const distance &d)
{
if(feet<=d.feet)
{
return true;
}
if((feet==d.feet)&&(inches<=d.inches))
{

[name]
[roll no]
return true;
}
return false;
}
bool operator >=(const distance &d)
{
if(feet>=d.feet)
{
return true;
}
if((feet==d.feet)&&(inches>=d.inches))
{
return true;
}
return false;
}
bool operator ==(const distance &d)
{
if((feet==d.feet)&&(inches==d.inches))
{
return true;
}
return false;
}
bool operator !=(const distance &d)
{
if((feet!=d.feet)&&(inches!=d.inches))
{
return true;
}
return false;
}
};
void main()
{
clrscr();
distance d1(11,10),d2(5,11);
distance d3(23,11),d4(12,11);
distance d5(11,11),d6(11,11);
distance d7(34,3),d8(3,5);
cout<<"D1 is ";
d1.show();
cout<<"D2 is ";
d2.show();

[name]
[roll no]
cout<<"D3 is ";
d3.show();
cout<<"D4 is ";
d4.show();
cout<<"D5 is ";
d5.show();
cout<<"D6 is ";
d6.show();
cout<<"D7 is ";
d7.show();
cout<<"D8 is ";
d8.show();
cout<<"OVERLOADING < OPERATOR==>"<<endl;
if(d1<d2)
{
cout<<"D1 is less than D2"<<endl;
}
else
{
cout<<"D2 is less than D1"<<endl;
}
cout<<"OVERLOADING > OPERATOR==>"<<endl;
if(d1>d2)
{
cout<<"D1 is greater than D2"<<endl;
}
else
{
cout<<"D2 is greater than D1"<<endl;
}
cout<<"OVERLOADING <= OPERATOR==>"<<endl;
if(d3<=d4)
{
cout<<"D3 is less than or equal to than D4"<<endl;
}
else
{
cout<<"D4 is less than or equal to than D3"<<endl;
}
cout<<"OVERLOADING >= OPERATOR==>"<<endl;
if(d3>=d4)
{
cout<<"D3 is greater than or equal to than D4"<<endl;
}

[name]
[roll no]
else
{
cout<<"D4 is greater than or equal to than D3"<<endl;
}
cout<<"OVERLOADING == OPERATOR==>"<<endl;
if(d5==d6)
{
cout<<"D5 is equal to than D6"<<endl;
}
else
{
cout<<"D5 is not equal to than D6"<<endl;
}
cout<<"OVERLOADING != OPERATOR==>"<<endl;
if(d7!=d8)
{
cout<<"D7 is not equal to than D8"<<endl;
}
else
{
cout<<"D7 is equal to than D3"<<endl;
}
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 24
Aim – Write a program to overload assignment(=) operator
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inches;
public:
Distance()
{
feet=0;
inches=0;
}
Distance(int f, int i)
{
feet=f;
inches=i;
}
void operator=(const Distance &D )
{
feet=D.feet;
inches=D.inches;
}
void displayDistance()
{
cout<<"F:"<<feet<<"\tI:"<<inches<<endl;
}
};
void main()
{
clrscr();
Distance D1(11, 10), D2(5, 11);
cout<<"First Distance:";
D1.displayDistance();
cout<<"Second Distance:";
D2.displayDistance();
D1 = D2;
cout<<"First Distance:";
D1.displayDistance();

[name]
[roll no]
getch();
}

EXPERIMENT – 25
[name]
[roll no]
Aim – Write a program to overload new and delete operators
#include<iostream.h>
#include<conio.h>
const int ARRAY=10;
class vector
{
public:
int *array1;
void *operator new(size_t size)
{
vector *d;
d=::new vector;
d->array1= new int[ARRAY];
return d;
}
void operator delete(void *vec)
{
vector *c;
c=(vector *)vec;
delete(int *)c->array1;
::delete vec;
}
void read()
{
for(int i=0;i<ARRAY;i++)
{
cout<<"vector ["<<i<<"]=?";
cin>>array1[i];
}
}
int sum()
{
int sums=0;
for (int i=0;i<ARRAY;i++)
sums+=array1[i];
return sums;
}
};
void main()
{
clrscr();
vector *d=::new vector;
cout<<"enter vector data.."<<endl;
[name]
[roll no]
d->read();
cout<<"sum of vector="<<d->sum();
delete d;
getch();
}

EXPERIMENT – 26

[name]
[roll no]
Aim – Write a program to define a function template for swapping two
items of the various data types such as integer, float and character
#include<iostream.h>
#include<conio.h>
template<class t>
void swap(t &v1,t &v2)
{
t temp;
temp=v1;
v1=v2;
v2=temp;
}
void main()
{
clrscr();
int i1,i2;
float f1,f2;
char c1,c2;
cout<<endl<<endl<<"DATA TYPE - INT"<<endl;
cout<<"Enter value of i1:";
cin>>i1;
cout<<"Enter value of i2:";
cin>>i2;
cout<<endl<<endl<<"DATA TYPE - FLOAT"<<endl;
cout<<"Enter value of f1:";
cin>>f1;
cout<<"Enter value of f2:";
cin>>f2;
cout<<endl<<endl<<"DATA TYPE - CHARACTER"<<endl;
cout<<"Enter value of c1:";
cin>>c1;
cout<<"Enter value of c2:";
cin>>c2;
cout<<"Press any key to continue....";
getch();
clrscr();
cout<<endl<<endl<<"DATA TYPE - INT"<<endl;
cout<<"Before swap()==>"<<endl;
cout<<"i1 is "<<i1<<" and i2 is "<<i2<<endl;
swap(i1,i2);
cout<<"After swap()==>"<<endl;
cout<<"i1 is "<<i1<<" and i2 is "<<i2<<endl;

[name]
[roll no]
cout<<endl<<endl<<"DATA TYPE - FLOAT"<<endl;
cout<<"Before swap()==>"<<endl;
cout<<"f1 is "<<f1<<" and f2 is "<<f2<<endl;
swap(f1,f2);
cout<<"After swap()==>"<<endl;
cout<<"f1 is "<<f1<<" and f2 is "<<f2<<endl;
cout<<endl<<endl<<"DATA TYPE - CHARACTER"<<endl;
cout<<"Before swap()==>"<<endl;
cout<<"c1 is "<<c1<<" and c2 is "<<c2<<endl;
swap(c1,c2);
cout<<"After swap()==>"<<endl;
cout<<"c1 is "<<c1<<" and c2 is "<<c2<<endl;
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 27
Aim – Write a program to define the function template for calculating
the square of the given number with different data types
#include<iostream.h>
#include<conio.h>
template<class t>
t square(t x)
{
t result;
result=x*x;
return result;
}
void main()
{
clrscr();
int i,ii;
float x,xx;
double y,yy;
cout<<"Enter integer value i:";
cin>>i;
cout<<"Enter float value x:";
cin>>x;
cout<<"Enter double value y:";
cin>>y;
ii=square(i);
xx=square(x);
yy=square(y);
cout<<"Square of "<<i<<" is "<<ii<<endl;
cout<<"Square of "<<x<<" is "<<xx<<endl;
cout<<"Square of "<<y<<" is "<<yy<<endl;
getch();
}

[name]
[roll no]
EXPERIMENT – 28
Aim – Write a program to illustrate how template functions can be
overloaded
#include<iostream.h>

[name]
[roll no]
#include<conio.h>
template<class t>
void print(t x)
{
cout<<x<<endl;
}
template<class t>
void print(t x,int n)
{
for(int i=0;i<n;i++)
{
cout<<x<<endl;
}
}
void main()
{
clrscr();
int x;
int n,c;
cout<<"Enter value of x:";
cin>>x;
cout<<"1.Print x one time"<<endl;
cout<<"2.Print x n times"<<endl;
cout<<"Enter Choice:";
cin>>c;
switch(c)
{
case 1:print(x);
break;
case 2:cout<<"Enter value of n:";
cin>>n;
print(x,n);
break;
default:cout<<"Wrong Choice";
};
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 29
Aim – Write a program to illustrate how to define and declare a class
template for reading two data items from the keyboard and find their
sum
#include<iostream.h>
#include<conio.h>
template<class t>
class test
{
private:
t a,b;
public:
test(t x, t y)
{
a=x;
b=y;
}
void show()
{
cout<<"\n Two numbers whose sum is to be calculated are: ";
cout<<"\n a: "<<a<<"\n b: "<<b;
cout<<"\n The Sum is: "<<a+b;
}
};
void main()
{
clrscr();
int a,b;
float c,d;
cout<<"\n Enter two integers:"<<endl;
cin>>a>>b;
cout<<"\n Enter two real numbers:"<<endl;
cin>>c>>d;
test <int> test1(a,b);
test <float> test2(c,d);
test1.show();
test2.show();
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 30
Aim – Write a program to demonstrate the use of special functions,
constructor and destructor in the class template. The program is used to
find the largest of two numbers entered
#include<iostream.h>
#include<conio.h>
template <class t>
class test
{
private:
t a,b;
public:
test(t x,t y)
{
a=x;
b=y;
}
void show()
{
if(a>b)
{
cout<<a<<" is greater than "<<b;
}
else
{
cout<<b<<" is greater than "<<a;
}
}
~test(){}
};
void main()
{
clrscr();
int a,b;
cout<<"\n Enter two numbers to find the biggest:"<<endl;
cin>>a>>b;
test <int> t1(a,b);
t1.show();
getch();
}

[name]
[roll no]
EXPERIMENT – 31
[name]
[roll no]
Aim – Write a program to read a set of lines from keyboard and store it
on a specified file
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n;
char data1[100],data2[100];
cout<<"Enter number of lines to be stored:";
cin>>n;
fstreamoutfile;
outfile.open("p31.dat",ios::out|ios::binary);
cout<<"Writing to the file"<<endl;
for(int i=1;i<=n;i++)
{
cout<<"Enter a sentence:";
gets(data1);
outfile.write((char*)&data1,sizeof(data1));
}
outfile.close();
fstreaminfile;
infile.open("p31.dat",ios::in|ios::binary);
cout<<"Reading from the file"<<endl;
while(infile.read((char*)&data2,sizeof(data2)))
{
puts(data2);
}
infile.close();
getch();
}

[name]
[roll no]
[name]
[roll no]
EXPERIMENT – 32
Aim – Write a program to read a text file and display its contents on the
screen
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n;
char data1[100],data2[100];
cout<<"Enter number of lines to be stored:";
cin>>n;
fstreamoutfile;
outfile.open("p32.txt",ios::out);
cout<<"Writing to the file"<<endl;
for(int i=1;i<=n;i++)
{
cout<<"Enter a sentence:";
gets(data1);
outfile<<data1<<endl;
}
outfile.close();
fstreaminfile;
infile.open("p32.txt",ios::in);
cout<<"Reading from the file"<<endl;
while(infile.getline(data2,100))
{
puts(data2);
}
infile.close();
getch();
}

[name]
[roll no]
EXPERIMENT – 33
[name]
[roll no]
Aim – Write a program to copy the contents of a file into another
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n;
char data1[100],data2[100],data3[100];
cout<<"Enter number of lines to be stored:";
cin>>n;
fstream outfile1;
outfile1.open("p33_1.txt",ios::out);
cout<<"Writing to first file"<<endl;
for(int i=1;i<=n;i++)
{
cout<<"Enter a sentence:";
gets(data1);
outfile1<<data1<<endl;
}
outfile1.close();
fstream infile1,outfile2;
infile1.open("p33_1.txt",ios::in);
outfile2.open("p33_2.txt",ios::out);
cout<<"Reading from the first file and copying to second file"<<endl;
while(infile1.getline(data2,100))
{
puts(data2);
outfile2<<data2<<endl;
}
infile1.close();
outfile2.close();
fstream infile2;
infile2.open("p33_2.txt",ios::in);
cout<<"Reading from copied file"<<endl;
while(infile2.getline(data3,100))
{
puts(data3);
}
infile2.close();
getch();
}
[name]
[roll no]
EXPERIMENT – 34

[name]
[roll no]
Aim – Write a program to read the class object of student_info such as
name, age, sex, height and weight from the keyboard and to store them
in a file using read() and write() functions. Again the same file is opened
for reading and displaying the contents of the file on the screen
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student_info
{
private:
char name[20];
int age;
char sex;
float height,weight;
public:
void input()
{
cout<<"Enter name:";
gets(name);
cout<<"Enter age:";
cin>>age;
cout<<"Enter sex(M/F):";
cin>>sex;
cout<<"Enter height(in cms):";
cin>>height;
cout<<"Enter weight(in kgs):";
cin>>weight;
}
void output()
{
cout<<"Name:";
puts(name);
cout<<"Age:"<<age<<endl;
cout<<"Sex:"<<sex<<endl;
cout<<"Height:"<<height<<endl;
cout<<"Weight:"<<weight<<endl;
}
};
void main()
{

[name]
[roll no]
clrscr();
student_info s1,s2;
fstreamoutfile;
outfile.open("p31.dat",ios::out|ios::binary);
cout<<"Writing to the file"<<endl;
s1.input();
outfile.write((char*)&s1,sizeof(s1));
outfile.close();
fstreaminfile;
infile.open("p31.dat",ios::in|ios::binary);
cout<<"Reading from the file"<<endl;
while(infile.read((char*)&s2,sizeof(s2)))
{
s2.output();
}
infile.close();
getch();
}

[name]
[roll no]

You might also like