Operator Overloading
Operator Overloading
Contents
• Operator Overloading
• Overloading Unary Operator
• Overloading Binary Operator
• Rules for overloading operator
• Type Conversion
uopoverload obj;
obj.getvalue();
++obj;
cout<<"Increment Complex Number\n";
obj.display();
++obj;
cout<<"Decrement Complex
Number\n";
obj.display();
return 0;
}
OR
#include <iostream>
int main()
using namespace std;
{
num n,p,q;
class num
n=p+q;
{
n.display();
int a=10;
return 0;
public:
}
num operator+(num x)
{
num y;
y.a=a+x.a;
return(y);
}
void display()
{
cout<<"Value is"<<a;
}
By: Kanika Sharma 13
};
Overloading with Friend Functions
• If overloading as a friend function ,binary operator
requires two argument
• Friend functions are useful when we require performing
an operation with operand of two different types.
• Friend function can be called without using an object.