Object Oriented Programming C++ Lecture No 10


  • Operator overloading
  • Overloading less than (<) operator
  • Implementing full support for an operator


Operator Overloading

  • How we can add functionality in our classes, so that they behave more like C++’s basic data types?
  • With operator overloading, we can arrange for the basic operators of C++ to operate in a well-defined way on our class objects.
  • Operator overloading allows us to apply standard operators (such as +,-,*,< and so on) to objects of our classes.
  • To do this, we write a function that redefines a particular operator so that it performs a particular action every time it is applied to objects of our classes.
    • We have seen Box class, with methods named volume() and compareVolume().
    • compareVolume() method allows us comparing two objects of Box class:

    –if(Box1.compareVolume(Box2) < 0)

    –//do something

    • Wouldn’t it be nice, if we could write:

    –if(Box1 < Box2)

    –//do something

    • In order to make it work, we need to write a function that defines the less-than operator’s behavior.
    • We can define the function to return true, if the volume of the first box is less than that of second.
  • The name of that function will be operator<( ).
  • In general, the name of a function that overloads a given operator is composed of the keyword operator, followed by the operator that we are overloading.
  • A function that overloads an operator for a class does not necessarily have to be a member of the class.
  • A binary operator implemented as a member function has one parameter.
  • And we add the following prototype for the function to overload the < operator to the Box class.


Implementing an Overloaded Operator

  • Since, we are implementing a comparison, the return type is bool.
  • Our operator function operator<() will be called as a result of comparing two Box objects using <.
  • The parameter is the right operand of the < operator.
  • And the left operand will correspond to the this pointer.

if(box1 < box2)

cout<<“box1 is less than box2”<<endl;

  • The condition of this if statement will result in our operator function being called.
  • It is equivalent to the function call box1.operator<(box2)


cout<<“box1 is less than box2”<<endl;


if(box1 < box2)……link too

The object pointed to by this

The argument to the operator function….this link tooo

bool Box::operator<(const Box& aBox)const


return this->volume() < aBox.volume();







Press any key to continue


Implementing full support for an operator

  • Implementing an operator such as < for a class creates an expectation.
  • We can write expressions like Box1 < Box2.
  • What about Box1 < 25.0 or 10 < Box2 ?
  • Our operator function operator<() won’t handle either of these.
  • When we overload an operator for a class, we need to consider the likely range of circumstances in which the operator might be used.
  • First add a function that compares the volume of a Box object (the Box object is the first operand) with a second operand of type double.

bool operator<(double aValue)const;

//compare Box volume < double value

  • The Box object will be passed to the function as the implicit pointer this, and the double value is passed as an argument.

inline bool Box::operator<(double aValue) const

  {return volume() < aValue;}

  • For an expression like 10 < box2, we can’t implement the operator as a member function.
  • Because a member function always provides the this pointer as the left operand, and the left operand in this expression is of type double.
  • We have two choices:

–To implement the function as a global operator function

–Or as a friend function

  • Since we don’t need to access any private members, we can implement it as an ordinary function.

inline bool operator<(const double aValue, const Box& aBox)

  {return aValue < aBox.volume();}

  • We now have three overloaded versions of the < operator for Box objects.
  • We can handle expressions like:

–box1 < box2

–box1 < 2.5*box2.volume()

–0.5*(box1.volume() + box2.volume()) < box3

  • Either operand for the < operator can be any expression that results in a value of type double, or a Box object.












Press any key to continue.

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − fifteen =