Operator Overloading in C++ – Pedagogy Zone

By
Last updated:

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:

Operator Overloading in C++

{
body of operator function;
}
eg : int operator + ();
void operator – ()

The process of operator overloading involves the following steps:

  1. Declare a class (that define the data type) whose objects are to be manipulated using operators.
  2. Declare the operator function, in the public part of the class. It can be either a normal member function or a friend function.
  3. 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

Santhakumar Raja

I am the founder of Pedagogy Zone, a dedicated education platform that provides reliable and up-to-date information on academic trends, learning resources, and educational developments.

For Feedback - techactive6@gmail.com