# Object Oriented Programming C++ Lecture No 10

# Outline

- 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

class Box { public: bool operator<(const Box& aBox) const; //overloaded less-than operator //the rest of the box class };

- 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)*

if(**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();

}

## Example

class Box{ private: double length; double breadth; double height; public: Box(double l=1.0,double b=1.0,double h=1.0); double volume() const; double getLength() const; double getBreadth() const; double getHeight() const; bool operator<(const Box& aBox) const {return volume() < aBox.volume();} };

#include"box.h" Box::Box(double l,double b,double h) :length(l),breadth(b),height(h){} double Box::volume()const {return length*breadth*height;} double Box::getLength() const {return length;} double Box::getBreadth() const {return breadth;} double Box::getHeight() const {return height;}

void main(){ Box boxes[10]; int i; for(i=0; i<10; i++) {boxes[i]=Box(i,i*2,i*3);} Box largest=boxes[0]; for(i=0; i<10; i++){ if(largest < boxes[i]) largest=boxes[i]; } cout<<largest.getLength()<<endl <<largest.getBreadth()<<endl <<largest.getHeight()<<endl; }

9

18

27

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
or*Box1 < 25.0*?*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__.

## Example

class Box{ private: double length; double breadth; double height; public: Box(double l=1.0,double b=1.0,double h=1.0); double volume() const; double getLength() const; double getBreadth() const; double getHeight() const; bool operator<(const Box& aBox)const;//compare Box<Box bool operator<(const double aValue)const; //compare Box<double value };

inline bool Box::operator<(const Box& aBox) const //compare Box<Box {return volume() < aBox.volume();} inline bool Box::operator<(const double aValue) const //compare Box<double value {return volume() < aValue;} inline bool operator<(const double aValue, const Box& aBox) //compare double value < Box {return aValue < aBox.volume();}

Box::Box(double l,double b,double h) :length(l),breadth(b),height(h){} double Box::volume()const {return length*breadth*height;} double Box::getLength() const {return length;} double Box::getBreadth() const {return breadth;} double Box::getHeight() const {return height;}

void main(){ Box boxes[10]; int i; for(i=0; i<10; i++) {boxes[i]=Box(i,i*2,i*3);} for(i=0; i<10; i++){ if(1000 < boxes[i] && boxes[i] < 4000) cout<<boxes[i].getLength()<<endl <<boxes[i].getBreadth()<<endl <<boxes[i].getHeight()<<endl<<endl; } }

6

12

18

7

14

21

8

16

24

Press any key to continue.