C++ Tutorial Home Page

C++ Operator Overloading

What is operator overloading
Rules for overloading Operators
Operator Overloading restrictions
List of operators that cannot be overloaded.
Overloading Unary operator
Overloading increment operator(++)
Overloading decrement operator(–)
Overloading unary operator(-)
Overloading increment operator(++) postfix
Overloading decrement operator(–) postfix
Overloading increment operator (++) with friend function
Overloading increment operator (–) with friend function
Increment the time using ++
Decrement the time using ++
Overloading Binary operator(+,-,*,/,%)
Add two object using “+” operator with friend function
Overloading “-” operator
Overloading “*” operator
Overloading “/” operator
Overloading “%” operator
overloading all (+,-,*,/,%)
Overloading “+” operator with friend function
class distance (feet,inches) with “+” operator
class distance (feet,inches) with “+” operator with friend function
class distance (km,m) with “+” operator
class distance (km,m) with “+” operator with friend function
class distance (m,cm) with “+” operator
class distance (m,cm) with “+” operator with friend function
class distance (km,m,cm) with “+” operator
class distance (km,m,cm) with “+” operator with friend function
class distance (km,m,cm,mm) with “+” operator
class distance (km,m,cm,mm) with “+” operator with friend function
class time(hh,mm,ss) with “+” operator
class time(hh,mm,ss) with “+” operator with friend function
class complex(real,ing) with “+” operator
class complex(real,ing) with “+” operator with friend function
class string(str) with “+” operator
class string(str) with “+” operator with friend function
overloading “+=” operator

C++:Operator Overloading 2

To overload an operator we use a special class member function called the “operator” function. The operator function has the following format.

Syntax:

returntype operator op(argument list);

Here,

* “operator” is a keyword.

* “op” is the operator to be overloaded , it has to be a valid c++ operator, we cannot overload any arbitrary symbol to perform some operation.

Rules for overloading Operators:

* Only existing operators can be overloaded.

* we cannot create a new operator and overload it.

* The operator must have at least one operand that is user defined type.

* We cannot change the basic meaning of an operator. i.e. we cannot redefine “+” operator to subtract or multiply two objects.

* The overloaded operator has to follow all the syntax rules of the original operator. They cannot be overridden/overlooked.

Overloading restrictions

There are certain restrictions in operator overloading

* We cannot create new operator system by overloading

* An overloaded operator should not violate the system rules for the original operator.

* To overload an operator , at least one of the operand should be a user defined type.

The following operators cannot be overloaded.

. (dot operator) direct membership operator
* (value at operator) member dereference operator
:: scope resolution operator
?: conditional operator / ternary operator
sizeof sizeof operator

Note:
It is the responsibility of the programmer to make sure that the overloaded operator performs the same function as the normal operator does.

Note:

Operators can be overloaded with and without using the friend function