Operator Overloading
 The process of defining additional meaning to an operator is called
operator overloading.
 It enables the operator to perform different operations depending on the
type of operands.
 It also enable the operators to process the user defined data types.
Example:
 The addition operator is used to add tow numeric values. Suppose a, b and
c are three integer variables. The following statement will add the
contents of a and b and store the result in variable c.
c = a + b;
 The addition operator already know how to process the integer operands.
Continue…
 But it does not know how to process to user defined objects. The above
statement generates an error if a, b and c are objects of a class. However,
operator overloading can enable the addition operator to manipulate two
objects.
Overloading an Operator
 An operator can be overloaded by declaring a special member function in
the class. The member function uses the keyword operator with the
symbol of operator to be overloaded.
Syntax:
 The syntax of overloading an operator is as follows:
return_type operator op()
{
function body;
}
Return_type: It indicates the type of value returned by the member
function.
Operator: It is the keyword that indicates that the member function is
used to overload an operator
Op: It is the symbol of an operator to be overloaded.
Overloading Unary Operators
 A type of operator that works with single operands is called unary
operator. The unary operators are overloaded to increase their
capabilities.
 The unary operators that can be overloaded in OOP using c++ are as
follows:
++ -- () -> new delete
Example: Overloading ++ operator
#include<iosteam> void operator ++()
Class count {
{ n = n+1;
private: }
int n; };
public: int main()
count() {
{ count obj;
n = 0; obj.show();
} ++obj;
void show() obj.show();
{ getch();
cout<<“n=”<<n<<endl; }
}
How above Program Works?
 The above program overloads the increment operator ++ to work to work
with all objects of user defined class count. It increases the value of data
member by 1. The user can use the same format to increase the value of
an object as used with integers.
Operator Overloading with returned value
 The increment operator can be used in assignment operator to store the
incremented value in another variable.
 Suppose a and b are two integers, the following statement will increment
the value of a by 1 then assign the value to b
b = ++a;
The above functionality can be assigned to the operator by returning the
new value from member function. The returned value can be stored in
another object of the same type on the left side of the assignment operator
Example:
#include<iostream>
class count
{
private:
int n, name ,rol_no;
public:
count()
{
n = 0;
}
void show()
{
cout<<“n=“<<n<<endl;
}
count operator ++()
{
count temp;
n = n+ 1;
temp.n = n;
return temp;
}
};
int main()
{
count x , y;
x.show();
y.show();
y = ++x;
x.show();
y.show();
}
Overloading Binary Operators
 A type of operators that works that works with operand is called binary
operators. These operators are overloaded to increase their capabilities.
 The binary operators that can be overloaded in OOP using c++ are as
follows:
== += + - * / % & << >>
-=
Example:
#include<iostream>
class add
{
private:
int a , b;
public:
add()
{
a = b = 0;
}
void input()
{
cout<<“ENTER a = “;
cin>>a;
cout<<“ENTER b = “;
cin>>b ;
}
void show()
{
cout<<“a=“<<a<<endl;
cout<<“b=“<<b<<endl;
}
add operator +(add p)
{
add temp;
temp.a = p.a+a;
temp.b = p.b+b;
return temp;
}
};
int main()
{
add x,y,z;
x.input();
y.input();
z = x + y;
x.show();
y.show();
z.show();
}

Operator overloading

  • 2.
    Operator Overloading  Theprocess of defining additional meaning to an operator is called operator overloading.  It enables the operator to perform different operations depending on the type of operands.  It also enable the operators to process the user defined data types.
  • 3.
    Example:  The additionoperator is used to add tow numeric values. Suppose a, b and c are three integer variables. The following statement will add the contents of a and b and store the result in variable c. c = a + b;  The addition operator already know how to process the integer operands.
  • 4.
    Continue…  But itdoes not know how to process to user defined objects. The above statement generates an error if a, b and c are objects of a class. However, operator overloading can enable the addition operator to manipulate two objects.
  • 5.
    Overloading an Operator An operator can be overloaded by declaring a special member function in the class. The member function uses the keyword operator with the symbol of operator to be overloaded.
  • 6.
    Syntax:  The syntaxof overloading an operator is as follows: return_type operator op() { function body; } Return_type: It indicates the type of value returned by the member function. Operator: It is the keyword that indicates that the member function is used to overload an operator Op: It is the symbol of an operator to be overloaded.
  • 7.
    Overloading Unary Operators A type of operator that works with single operands is called unary operator. The unary operators are overloaded to increase their capabilities.  The unary operators that can be overloaded in OOP using c++ are as follows: ++ -- () -> new delete
  • 8.
    Example: Overloading ++operator #include<iosteam> void operator ++() Class count { { n = n+1; private: } int n; }; public: int main() count() { { count obj; n = 0; obj.show(); } ++obj; void show() obj.show(); { getch(); cout<<“n=”<<n<<endl; } }
  • 9.
    How above ProgramWorks?  The above program overloads the increment operator ++ to work to work with all objects of user defined class count. It increases the value of data member by 1. The user can use the same format to increase the value of an object as used with integers.
  • 10.
    Operator Overloading withreturned value  The increment operator can be used in assignment operator to store the incremented value in another variable.  Suppose a and b are two integers, the following statement will increment the value of a by 1 then assign the value to b b = ++a; The above functionality can be assigned to the operator by returning the new value from member function. The returned value can be stored in another object of the same type on the left side of the assignment operator
  • 11.
    Example: #include<iostream> class count { private: int n,name ,rol_no; public: count() { n = 0; } void show() { cout<<“n=“<<n<<endl; } count operator ++() { count temp; n = n+ 1; temp.n = n; return temp; } }; int main() { count x , y; x.show(); y.show(); y = ++x; x.show(); y.show(); }
  • 12.
    Overloading Binary Operators A type of operators that works that works with operand is called binary operators. These operators are overloaded to increase their capabilities.  The binary operators that can be overloaded in OOP using c++ are as follows: == += + - * / % & << >> -=
  • 13.
    Example: #include<iostream> class add { private: int a, b; public: add() { a = b = 0; } void input() { cout<<“ENTER a = “; cin>>a; cout<<“ENTER b = “; cin>>b ; } void show() { cout<<“a=“<<a<<endl; cout<<“b=“<<b<<endl; } add operator +(add p) { add temp; temp.a = p.a+a; temp.b = p.b+b; return temp; } };
  • 14.
    int main() { add x,y,z; x.input(); y.input(); z= x + y; x.show(); y.show(); z.show(); }