Unary Operator and Binary Operator Overloading
Unary Operator and Binary Operator Overloading
Title: Write a C++ menu driven program to implement unary operator overloading (prefix and
postfix increment operator) and binary operator overloading (+ and <).
Description:
In C++, we can change the way operators work for user-defined types like objects and structures.
This is known as operator overloading. Using operator overloading in C++, you can specify
more than one meaning for an operator in one scope. The purpose of operator overloading is to
provide a special meaning of an operator for a user-defined data type.
With the help of operator overloading, you can redefine the majority of the C++ operators. You
can also use operator overloading to perform different operations using one operator.
To overload a C++ operator, you should define a special function inside the Class as follows:
class class_name
{
... .. ...
public
return_type operator symbol (argument(s))
{
... .. ...
}
... .. ...
};
Here is an explanation for the above syntax:
Hint:
Menu:
Press 1 to apply prefix increment operator on the object of class complex
Press 2 to apply postfix increment operator on the object of class complex
Press 3 to add two objects of class complex (using friend function)
Press 4 to compare two complex numbers using < operator
Binary operator overloading to add two complex numbers using friend function:
Friend function can also be used in place of member functions for overloading a binary operator,
the only difference being that a friend function requires two arguments to be explicitly passed to
it, while a member function requires only one.
In the complex number program discussed in the previous section, the statement:
c3=c1+c2;
is equivalent to:
c3= operator+(c1,c2);
Binary operator overloading to compare two complex numbers using member function:
bool operator < (complex c)
{…
…
}
Program Code:
#include<iostream>
class Complex{
int feet,inches;
PP Lab Name: PRN:
public:
Complex(){
feet=0;
inches=0;
feet=x;
inches=y;
++feet;
++inches; //prefix
feet--;
inches--;
Complex temp;
temp.feet=feet+c.feet;
PP Lab Name: PRN:
temp.inches=inches+c.inches;
return temp;
if(feet<obj.feet){
return true;
return true;
return false;
void display(){
cout<<feet<<"ft "<<inches<<"in"<<endl;
};
int main()
{
int a,b,c,d;
cin>>a>>b;
Complex c1(a,b);
c1.display();
cin>>c>>d;
Complex c2(c,d);
c2.display();
Complex c3;
int choice;
cout<<"case 1=Result after applying prefix increment operator on object of class
complex\n\n";
cout<<"case 2=Result after applying postfix increment operator on object of class
complex\n\n";
cout<<"case 3=Addition\n\n";
cout<<"case 4=Comparing\n\n";
cout<<"Enter your choice:";
cin>>choice;
switch(choice){
case 1://Result after applying prefix increment operator on object of class complex:
c1.display();
++c1;
c1.display();
PP Lab Name: PRN:
break;
case 2://Result after applying postfix increment operator on object of class complex:
c2.display();
c2--;
c2.display();
break;
cout<<"Addition is:"<<endl;
c3=c1+c2;
c3.display();
break;
cout<<"Comparing :"<<endl;
if(c1<c2)
{
}
PP Lab Name: PRN:
else{
break;
return 0;
Practice programs: Write a C++ program for Unary logical NOT (!) operator overloading.