O'Reilly logo

Object Oriented Programming with C++, Second Edition by Sunil Patekar, Mahesh Bhave

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required