The operator overloading feature of C++ is one of the methods of realizing polymorphism. C++ has the ability to provide the operators with a special meaning to an operator is known as operator overloading.
We can overload all the C++ operators except the following:
- Size resolution operator (::)
- Scope of operator (size of)
- Conditional operator (?:)
- Class member access operator (., .*, →*)
- Pointer to member declarator (::*)
Defining Operator Overloading
The general format of operator overloading is as follows:
{
body of operator function;
}
eg : int operator + ();
void operator – ()
The process of operator overloading involves the following steps:
- Declare a class (that define the data type) whose objects are to be manipulated using operators.
- Declare the operator function, in the public part of the class. It can be either a normal member function or a friend function.
- Define the operator function either within the body of a class or outside the body of the class.
Syntax for invoking the overloaded unary operator function is as follows:
Object operator
Operator object
eg : S++;
++S;
Syntax for invoking the overloaded binary operator function is as follows:
eg : S1 + S2 invokes the overloaded member function + of the S1 object’s class by passing S2 as the argument.
Read More Topics |
Const member function in C++ |
Default copy constructor in C++ |
The HyperText Transfer Protocol |
Five layer reference model in computer network |
Unique nature of web apps |