C++ programs
Write a C++ program to implement operator overloading –unary, binary, relational, insertion, extraction operator.
Write a C++ program to implement operator overloading –unary, binary, relational, insertion, extraction operator.
PROGRAM:
• UNARY:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
class counter
{
protected :
unsigned int count;
public :
counter()//constructor,no args
{
count=0;
}
counter(int c) //constructor one arg
{
count=c;
}
unsigned int get_count() const //return count
{
return count;
}
counter operator ++() //incr count(prefix)
{
return counter(++count);
}
};
class countdn : public counter
{
public :
countdn() : counter() //constructor,no args
{ }
countdn(int c) : counter(c) //constructor one arg
{ }
countdn operator --() //decr count(prefix)
{
return countdn(--count);
}
};
void main()
{
countdn c1; //class countdn
countdn c2(100);
clrscr();
cout<<"www.adkool.com";
cout<<"\n\nc1 = "<<c1.get_count();//display
cout<<"\n\nc2 = "<<c2.get_count();//display
++c1;//increment c1
++c1;
++c1;
cout<<"\n\nc1 = "<<c1.get_count(); //display c1
--c2; //decrement
--c2;
cout<<"\n\nc2 = "<<c2.get_count();//display c2
countdn c3=--c2; //creates c3 from c2
cout<<"\n\nc3 = "<<c3.get_count();//display c3
cout<<"www.adkool.com";
cout<<endl;
getch();
}
OUTPUT
c1 = 0
c2 = 100
c1 = 3
c2 = 98
c3 = 97
• BINARY
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class matrix
{
private :
int arr[3][3];
public :
void getmatrix();
matrix operator * (matrix);
void matrix_display();
};
void matrix : : getmatrix()//function to get matrix elements
{
int i,j;
cout<<"enter matrix elements";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
void matrix ::matrix_display()//function to display matrix elements
{
int i,j;
cout<<endl<<endl;
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<setw(5)<<arr[i][j];
}
}
matrix matrix : : operator * (matrix a) /*operator overload for multiplication of 2 matrices*/
{
matrix c;
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c.arr[i][j]=0;
for(k=0;k<3;k++)
{
c.arr[i][j]+=arr[i][k]*a.arr[k][j];
}
}
}
return c;
}
void main()
{
clrscr();
matrix a,b,c;
a.getmatrix();//get matrix elements
b.getmatrix();
c=a*b; //matrix multiplication using operator overloading
cout<<endl<<"product of 2 matrices"<<endl;
c.matrix_display();
getch();
}
OUTPUT
enter matrix elements
1 1 1
1 1 1
1 1 1
enter matrix elements
1 1 1
1 1 1
1 1 1
product of 2 matrices
3 3 3
3 3 3
3 3 3
• RELATIONAL OPERATOR
#include<iostream.h>
#include<conio.h>
class circle
{
int r;
public :
circle () : r(0)
circle(int r)
{
this->r=r;
}
int operator < (circle T)
{
if(r > T.r)
return 1;
else
return 0;
}
void area()
{
cout<<”Area = “<<3.14*r*r;
}
};
void main()
{
circle c1(5),c2(10);
clrscr();
if(c1 < c2)
c1.area();
else
c2.area();
getch();
}
OUTPUT
Area = 314
• INSERTION AND EXTRACTION OPERATOR
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
class distance
{
private :
int feet;
float inches;
public :
distance() : feet(0),inches(0.0) //constructor with no args
{}
distance(int ft,float in) : feet(ft),inches(in) //constructor with two args
{}
friend istream& operator >>(istream& s,distance& d);
friend ostream& operator <<(ostream& s,distance& d);
};
istream& operator >> (istream& s,distance& d) //get data from file or keyboard
{
char ch;
s>>d.feet>>ch>>ch>>d.inches>>ch; //overload >> operator
return s;
}
ostream& operator << (ostream& s,distance& d) //send data to file or screen
{
s<<d.feet<<”\’-“<<d.inches<<’\”’; //overload << operator
return s;
}
void main()
{
char c;
distance d1;
ofstream ofile; //create and open output stream
clrscr();
ofile.open(“dist.txt”);
do
{
cout<<”\nEnter distance : “;
cin>>d1; //get distance from user
ofile<<d1; //write it to output stream
cout<<”\nWant to enter more details (y/n)?”;
cin>>c;
}while(c!=’n’);
ofile.close(); //close output stream
ifstream ifile; //create and open input stream
ifile.open(“dist.txt”);
cout<<”\nContents of file are : \n”;
while(1)
{
ifile>>d1; //read distance from stream
if(ifile.eof()) //quit on EOf
break;
cout<<”Distance : “<<d1<<endl; //display distance
}
getch();
}
OUTPUT
Enter distance : 1 2 3 4 5
Want to enter more details (y/n)?y
Enter distance : 9 8 7 6 5
Want to enter more details (y/n)?n
Contents of file are :
Distance : 1’-4”
Distance : 9’-6”
DIST.TXT
1’-4”9’-6”