Use the operator overloading technique to define the binary operator+ in the Square class of Q1 then overload the insertion operator << for the same class, and overload the decrements operator-- which decrease the side by one . Use the new + operator to add two squares such that the side of resulted square equal to the addition of the sides of the added squares and use the decrements operator-- to decrease the side of square by one, then use the overloaded insertion operator << to print the resulted square in the main program. After that add a new integer pointer variable called color that point to an integer array of three elements to the class square then write the copy constructor for the new class.
#include <iostream>
class Square {
public:
int a, b;
Square(int a, int b) {
this->a = a;
this->b = b;
}
void operator--() {
if (this->a) this->a--;
if (this->b) this->b--;
}
ostream & operator<<(ostream & out, const Point & point) {
out << "[" << this->x << "," << this->y << "]";
return out;
}
};
Square operator+ (const Square &a, Square &b) {
return Square(a->a+b->a, a->b+b->b);
}
Comments
Leave a comment