Page 1 of 1

Please help write the following program in C++ In the previous week, you should have created a class called Rectangle.

Posted: Thu Jul 14, 2022 2:28 pm
by answerhappygod
Please help write the following program in C++
In the previous week, you should have created a class calledRectangle. This week, you will perform inheritance with theRectangle class.
Some Background: Rugs have a set size and are sold for aset price. On the other hand, Carpets are sold by cutting aswath from a roll -- so the size of a carpet (and its cost) can bevariable.
Create a class called Rug that inherits from Rectangle. Then give the Rug class a private price variable. Thelength, width and price of the Rug should be set in a constructorof the Rug class. Give the Rug class a getPrice method thatreturns the price. Also, override the inherited method thatreturns a string that describes the Rug object. That methodshould include the length, width, area, and price.
Create another class called Carpet that also inherits fromRectangle. It should have a price_per_square_foot variable. The length, width, and price_per_square_foot are set in theconstructor of the Carpet class. Give the Carpet glass agetPrice method that calculates the price of the Carpet and returnsit. Also, override the inherited method that returns a stringthat describes the Carpet object. That method should includethe length, width, area, price per square foot, andprice.
Then write a main method in which a Rug object and a Carpetobjects are created. Then print descriptions of theobjects
Rectangle class:
# include <iostream>#include <string>using namespace std;class Rectangle{private: double height = 0; double width = 0;
public: Rectangle() { height = 1; width = 1; } Rectangle(double h, double w) { setHeight(h); setWidth(w); } void setHeight(double h) { if (h < 1) { h =1; } height = h; } double getHeight() { return height;
} void setWidth(double w) { if (w < 1) { w =1; } width = w; } double getwidth() { return width;
} //Area and perimeter are calculated using heightand withd variable double getArea() { double Area = height *width; return Area; } double getPerimeter() { double Perimeter = 2 *(height + width); return Perimeter; }
string getString() { string temp = "The object isa Rectangle height " + to_string(height) + ", Width: " +to_string(width) + ", Perimeter: " + to_string(getPerimeter()) + ",Area: " + to_string(getArea()); return temp;
}
};
int main() { Rectangle r1; Rectangle r2(6, 3);
cout << " Height of ractangle object r1is:" << r1.getHeight() << ", Width is " <<r1.getwidth() << ", Perimeter: " << r1.getPerimeter()<< ", Area is : " << r1.getArea() << endl; cout << " Height of ractangle object r2is:" << r2.getHeight() << ", Width is " <<r2.getwidth() << ", Perimeter: " << r2.getPerimeter()<< ", Area is : " << r2.getArea() << endl;
r1.setHeight(8); r1.setWidth(5);
cout << " Height of ractangle object r1created with default constructor and then modified with mutatormethod:" << r1.getHeight() << ", Width is " <<r1.getwidth() << ", Perimeter: " << r1.getPerimeter()<< ", Area is : " << r1.getArea() << endl;
cout << r1.getString() <<endl; cout << r2.getString() << endl;
}