there is no error in main () complete the class Rectangle and its functions.
Int main ()
{
Rectangle rect1(3.4, 5.68);
Rectangle rect2(2.4, 4.68);
if (rect1-- != ++rect2) //increment/decrement will add & subtract by 1 of each data members of class
cout << ”\n Both are not of same size”;
else
cout << ”\n Both are of same size”;
rect1.show(); \\will display values 2.4, 4.68
Rectangle rect3 = rect1 + rect2;
rect3.show(); \\will display values 5.8, 10.36
system(“pause”);
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
class Rectangle{
float width, height;
public:
Rectangle(){}
Rectangle(float w, float h): width(w), height(h){}
Rectangle operator--(int){
--width;
--height;
return *this;
}
Rectangle operator++(){
++width;
++height;
return *this;
}
Rectangle operator+(const Rectangle &other){
return Rectangle(this->width + other.width, this->height + other.height);
}
bool operator!=(const Rectangle &other){
return !(this->width == other.width && this->height == other.height);
}
void show(){
cout<<endl<<width<<" "<<height<<endl;
}
};
int main (){
Rectangle rect1(3.4, 5.68);
Rectangle rect2(2.4, 4.68);
if (rect1-- != ++rect2) //increment/decrement will add & subtract by 1 of each data members of class
cout << "\n Both are not of same size";
else
cout << "\n Both are of same size";
rect1.show(); //will display values 2.4, 4.68
Rectangle rect3 = rect1 + rect2;
rect3.show(); //will display values 5.8, 10.36
system("pause");
return 0;
}
Comments
Leave a comment