Operator Overloading
Operator Overloading
1 write a program to negate values of two variables contained in an object. By overloading the unary
operator i.e. -.
#include<iostream>
class Negate
int x,y;
public:
void read()
cin>>x>>y;
x=-x;
y=-y;
void display()
cout<<"X="<<x<<"Y="<<y<<endl;
};
int main()
Negate n;
n.read();
-n;
n.display();
return 0;
Output:
C:\Users\Dhiraj\Desktop\Overloading>g++ Negate.cpp -o k
C:\Users\Dhiraj\Desktop\Overloading>.\k
X=-4Y=-5
#include<iostream>
class IncDec
int x,y;
public:
void read()
x--;
y--;
x++;
y++;
void display()
cout<<"X="<<x<<endl<<"Y="<<y<<endl;
};
int main()
IncDec n;
n.read();
--n;
n.display();
++n;
++n;
return 0;
Output:
C:\Users\Dhiraj\Desktop\Overloading>.\k
X=3
Y=4
X=5
Y=6
3. write a program to overload binary operator “+” to add two complex numbers.
#include<iostream>
class Complex
int x,y;
public:
void read()
cin>>x>>y;
Complex c1;
c1.x=x+c.x;
c1.y=y+c.y;
return c1;
void display()
if(y<0)
cout<<x<<y<<"i";
else{
cout<<x<<"i"<<y;
};
int main()
Complex c1;
Complex c2;
Complex c3;
c1.read();
c2.read();
c3=c1+c2;
c3.display();
return 0;
Output:
C:\Users\Dhiraj\Desktop\Overloading>g++ Oerat.cpp -o k
C:\Users\Dhiraj\Desktop\Overloading>.\k
9i9
4. write a program to overload binary operator “+=” to add two complex numbers
#include<iostream>
class Complex
int x,y;
public:
void read()
cin>>x>>y;
x=x+c.x;
y=y+c.y;
void display()
if(y<0)
cout<<x<<y<<"i";
else{
cout<<x<<"+i"<<y;
};
int main()
Complex c1;
Complex c2;
Complex c3;
c1.read();
c2.read();
c1+=c2;
c1.display();
return 0;
Output:
C:\Users\Dhiraj\Desktop\Overloading>g++ Z.cpp -o k
C:\Users\Dhiraj\Desktop\Overloading>.\k
6+i8
5. write a program to add two distance entered by the user in feet and inches using overloaded binary
operator “+”.
#include<iostream>
#include<conio.h>
class Distance
int ft,in;
public:
void read()
cin>>ft>>in;
}
Distance operator +(Distance d)
Distance dr;
dr.ft=ft+d.ft;
dr.in=in+d.in;
if(dr.in>=12){
dr.ft++;
dr.in=dr.in-12;
return dr;
void display()
};
int main()
Distance d1;
Distance d2;
Distance d3;
d1.read();
d2.read();
d3=d1+d2;
d3.display();
return 0;
}
Output:
C:\Users\Dhiraj\Desktop\Overloading>.\k
11
C:\Users\Dhiraj\Desktop\Overloading>
6.write a program to declare class Jkurnal having data mmbers as journal name,price and no-of-
pages.accept this data for two object and display the name of journal having greatest price
#include<iostream>
#include<conio.h>
class Journal
private:
char name[20];
int pages,price;
public:
void accept()
cin>>name>>pages>>price;
}
void display()
return x;
};
int main()
Journal a,b,c;
a.accept();
b.accept();
c=a>b;
c.display();
return 0;
Output:
C:\Users\Dhiraj\Desktop\Overloading>g++ Journal.cpp -o l
C:\Users\Dhiraj\Desktop\Overloading>.\l
4
400
200
Name:a
Pages:4
Price400
C:\Users\Dhiraj\Desktop\Overloading>
7. Write a program to declare class Distance having data members feet and inch. Overloaded unary –
operator so that when it is used with object of this class it will decrement value of inches by 1.
#include<iostream>
#include<conio.h>
#include<stdio.h>
class Distance1
private:
int ft,inch;
public:
void accept()
cin>>ft>>inch;
}
void display()
cout<<ft<<"feet"<<inch<<"inches\n";
inch=inch-1;
if(inch==-1)
inch=11;
ft=ft-1;
};
int main()
Distance1 d;
d.accept();
d.display();
-d;
cout<<"After Decrementing\n";
d.display();
return 0;
Output:
C:\Users\Dhiraj\Desktop\Overloading>g++ Distance1.cpp -o l
C:\Users\Dhiraj\Desktop\Overloading>.\l
3feet0inches
After Decrementing
2feet11inches
8 Write a program to declare class Distance having data members feet and inch. Overloaded binary ++
operator so that when it is used with object of this class it will increment value of inches by 1
#include<iostream>
#include<conio.h>
#include<stdio.h>
class Distance2
private:
int ft,inch;
public:
void accept()
cin>>ft>>inch;
void display()
cout<<ft<<"feet"<<inch<<"inches\n";
}
void operator -()
inch=inch+1;
if(inch==12)
inch=0;
ft=ft+1;
};
int main()
Distance2 d;
d.accept();
d.display();
++d;
cout<<"After Decrementing\n";
d.display();
return 0;
Output:
C:\Users\Dhiraj\Desktop\Overloading>g++ Distance2.cpp -o l
C:\Users\Dhiraj\Desktop\Overloading>.\l
11
3feet11inches
After Decrementing
4feet0inches
9 write a program to add two complex number using operator overloaded by a friend function
#include<iostream>
#include<conio.h>
class Complex
int x,y;
public :
void read()
cin>>x>>y;
void display()
if(y<0)
cout <<x<<y<<"i";
else
cout<<x<<"+i"<<y;
}
};
Complex c;
c.x=c1.x+c2.x;
c.y=c1.y+c2.y;
return c;
int main()
Complex c1;
Complex c2;
Complex c3;
c1.read();
c2.read();
c3=c1+c2;
c3.display();
return 0;