Answer to Question #213914 in C++ for Imran hashmi

Question #213914

Assuming 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;

}



1
Expert's answer
2021-07-05T07:40:15-0400
#include <iostream>
#include <cstdlib>
using namespace std;
class Rectangle{
    float width, length;
    public:
    Rectangle(){}
    Rectangle(float w, float l): width(w), length(l){}
    Rectangle operator--(int){
        *this = Rectangle(--width, --length);
        return *this;
    }
    Rectangle operator++(){
        *this = Rectangle(++width, ++length);
        return *this;
    }
    Rectangle operator+(const Rectangle &that){
        return Rectangle(this->width + that.width, this->length + that.length);
    }
    bool operator!=(const Rectangle &that){
        return !(this->width == that.width && this->length == that.length);
    }
    void show(){
        cout<<endl<<width<<" "<<length<<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;
}      

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS