READ the notes and understand the example given below before you start the Exercises.

### Exercises

see slide 12 from the Lecture 1 where we discussed operator overloading

Exercise 1: Modify the Circle class which you have written earlier to overload the + operator so that you can add two Circle objects. Adding two Circle object should give another Circle whose radius the sum of the radii of the two Circle objects.

Exercise 2: Modify the Rectangle class which was shown to you as an example earlier to overload the + operator so that you can add two Rectangle objects. Adding two Rectangle objects should give another Rectangle object whose length is the sum of the lengths of the two Rectangle objects and whose breadth is the sum of the breadths of the two Rectangle objects.

Exercise 3: Write a class Time which represents time. the class should have three fields for hours, minutes and seconds. It should have constructor to initialize the hours, minutes and seconds.
A method printTime() to print the current time.
Overload the following operators:
plus operator (+) (add two time objects based on 24 hour clock)
and < (compare two time objects)

Exercise 4: For the above Rectangle overload the < (less than) operator so that you can compare two Rectangles as to which is bigger (or smaller) . The logic you will use is to compare the areas of the Rectangle objects and decide which is bigger.

Exercise 5: For the above Circle overload the > (greater than) operator so that you can compare two Circles as to which is bigger (or smaller) . The logic you will use is to compare the radii of the Circle objects and decide which is bigger.

### Notes

C++ allows operators to be overloaded which means you can implement your own functionality for these operators in your classes so that these operators can be used on objects.

Most operators can be overloaded including =, +,-,/,*,%,^,<,>,<=,>=,!=,…

For example, assume that you that have a class Square and you create two objects of it as follows:

```Square s1(3);
Square s2(4);
```

You can add the s1 and s2 objects to get another Square object using the + operator as shown below provided that you have overloaded it in the Square class.

```Square s = s1 + s2;
```

To overload the + operator you will first define the operator in the class definition as follows:

```Square operator +(Square);
```

and the implement it in the class as show by the following code

```//example of operator overloading
//the new square has a side which is sum of the current object and sq object
Square Square :: operator +(Square sq){
Square squareTemp(0);
squareTemp.side = side + sq.side;
return squareTemp;
}
```

### Full working example

Complete code for the Square class with + operator overloaded is shown below

```#include <iostream>
using namespace std;

class Square {
private:
int side;
public:
Square(int);
Square operator +(Square);
int getSide();
int getArea();
};

Square::Square(int _side){
side = _side;
}

//the new square has a side which is sum of the current object and sq object
Square Square :: operator +(Square sq){
Square squareTemp(0);
squareTemp.side = side + sq.side;
return squareTemp;
}

int Square :: getSide(){
return side;
}

int Square :: getArea(){
return side * side;
}

int main(){
Square s1(3);
Square s2(4);

Square s = s1 + s2;
cout << "Side of s:" << s.getSide() << endl;
cout << "Area of s:" << s.getArea() << endl;
}
```
page revision: 10, last edited: 11 Oct 2009 12:57