Coupon Accepted Successfully!



Operator overloading is one of the important and useful features of C++. We are familiar with function overloading in which multiple functions use the same name. The concept of operator overloading is somewhat similar to that of function overloading.

A symbol that is used to perform an operation is called an operator. It is used to perform an operation with constants and variables. A programmer cannot build an expression without an operator.

C++ frequently uses user-defined data types that are a combination of one or more basic data types. C++ has the facility to create/build user-defined data type. User-defined data types created from class or struct are nothing but a combination of one or more variables of basic data types. The compiler knows how to perform various operations using operators for the built-in types; however, for the objects those are instance of the class, the operation routine must be defined by the programmer.

For example, in traditional programming languages the operators such as +, −, <=, >=, etc. can be used only with basic data types such as int or float. The operator + (plus) can be used to perform addition of two variables, but the same is not applicable for objects. The compiler cannot perform addition of two objects. The compiler would throw an error if addition of two objects is carried out. The compiler must be made aware of the addition process of two objects. When an expression including operation with objects is encountered, a compiler searches for the definition of the operator, in which a code is written to perform an operation with two objects. Thus, to perform an operation with objects we need to redefine the definition of various operators. For example, for addition of objects A and B, we need to define operator + (plus). Redefining the operator plus does not change its natural meaning. It can be used for both variables of built-in data type and objects of user-defined data type.

Operator overloading is one of the most valuable concepts introduced by C++ language. It is a type of polymorphism. Polymorphism permits to write multiple definitions for functions and operators. C++ has a number of standard data types such as int, float, char, etc. The operators +, −, *, and = are used to carry operations with these data types. Operator overloading helps the programmer to use these operators with the objects of classes. The outcome of operator overloading is that the objects can be used in as natural manner as the variables of basic data type. Operator overloading provides the capability to redefine the language in which the working operator can be changed


Consider an example,

where, a, c, and d are variables of basic data types like int or float. The use of operator +, −, and = is valid. However, if we try these operators with the object, the compiler displays an error message “Illegal structure operation.”

For example, A, B, and C are three objects of class number. Each object holds an individual copy of member variable x and y. We want to perform addition of A and B and store the result in C. For the sake of understanding, the member variables are declared in public section. The addition of A and B implies addition of member variables of A and member variables of B. The result of this operation will be stored in member variables of C. This feature can be implemented as follows

10.1 Write a program to perform addition of two objects and store the result in third object. Display contents of all the three objects.

Explanation: In the above program, A, B, and C are objects of class number. Using constructor, objects are initialized. Consider the following statements:



In the above statements, addition of members of objects A and B is performed and stored in C. Each member variable is accessed individually and stored in member variable of C. For example, member x of A and member x of B are added and stored in x of C. Similarly, addition of the other members is carried out.

In this program we cannot perform operation C=A+B. The operation with objects is complicated because it involves operation of one or more data member variables which are part of objects.


The capability to relate the existing operator with a member function and use the resulting operator with objects of its class as its operands is called operator overloading.

Test Your Skills Now!
Take a Quiz now
Reviewer Name