7.3 BINARY OPERATOR OVERLOADING

Just like unary operators, binary operators can also be overloaded. A binary operator takes two operands. The rule in operator overloading is that one of the operands must be an object. (Well in its absence, you are redefining what is 2 + 2. No language designer will allow you to do that.)

This leads to three distinct cases or types of binary operator overloading.

Case (1) Object + object

Case (2) Object + basic data type (say int )

Case (3) Basic data type (say int ) + object

Let us discuss all these cases one by one.

7.3.1 Case 1: object + object

It is natural to assume that both objects belonging to same class. Consider that we want to overload operator + with both objects of class Complex. It will imply that ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.