2 (20%) write aprogram, in cm. Define a Shape class with two member variables wi
ID: 3907375 • Letter: 2
Question
2 (20%) write aprogram, in cm. Define a Shape class with two member variables width and height both are ble; define a public member function named set0, which has two arguments hat can be assigned to the two member variables correspondingly. Then define two sub-classes Triangle and Rectangle, both inherit fr om shape and both have a member function area0 that the area of the set function of the objects respectively, and then call area) function of the two objects and output both shape. In the main) function, define two objects of Triangle and Rectangle and then call Noes Ihc ard or roetanele is widb iecieh, and riangle is width * height /2. Note: All member functions should be in-line functions #include using namespace std; //Here you write the three classes Shape, Rectangle, and Triangle int mainO Rectangle rect; Triangle tri; rect.set(1,2);tri.set(3,4); coutExplanation / Answer
class Shape { protected: double width; double height; public: void set(double w, double h) { width = w; height = h; } }; class Rectangle : public Shape { public: double area() { return width * height; } }; class Triangle : public Shape { public: double area() { return 0.5 * width * height; } };
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.