<\/span><\/h2>\n\n\n\nIn this challenge, you are required to compute the area of a rectangle using classes.<\/p>\n\n\n\n
Create two classes:<\/p>\n\n\n\n
Rectangle<\/strong><\/p>\n\n\n\nThe Rectangle<\/em> class should have two data fields-width<\/em> and height<\/em> of int<\/em> types. The class should have display()<\/em> method, to print the width<\/em> and height<\/em> of the rectangle separated by space.<\/p>\n\n\n\nRectangleArea<\/strong><\/p>\n\n\n\nThe RectangleArea<\/em> class is derived from Rectangle<\/em> class, i.e., it is the sub-class of Rectangle<\/em> class. The class should have read_input()<\/em> method, to read the values of width<\/em> and height<\/em> of the rectangle. The RectangleArea<\/em> class should also overload the display()<\/em> method to print the area (width x height)<\/strong> of the rectangle.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first and only line of input contains two space separated integers denoting the width and height of the rectangle.<\/p>\n\n\n\n
<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= width, height<\/em> <= 100<\/strong><\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nThe output should consist of exactly two lines:
In the first line, print the width<\/em> and height<\/em> of the rectangle separated by space.
In the second line, print the area<\/em> of the rectangle.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n10 5<\/code><\/pre>\n\n\n\nSample Output <\/strong><\/p>\n\n\n\n10 5\n50<\/code><\/pre>\n\n\n\nExplanation <\/strong><\/p>\n\n\n\nAs, width<\/em> = 10<\/strong> and height<\/em> = 5<\/strong>, so area<\/em> = width<\/em> x height<\/em> = 50<\/strong><\/p>\n\n\n\n<\/span>Solution – Rectangle Area in C++<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <iostream>\n\nusing namespace std;\n\/*\n * Create classes Rectangle and RectangleArea\n *\/\nclass Rectangle\n{\n protected:\n int width;\n int height;\n public:\n void read_input()\n {\n cin>>width>>height;\n }\n void display()\n {\n cout<<width<<\" \"<<height<<\"\\n\";\n } \n};\nclass RectangleArea :public Rectangle\n{\n public:\n void display()\n {\n cout<<width * height;\n }\n}; \n\n\nint main()\n{\n \/*\n * Declare a RectangleArea object\n *\/\n RectangleArea r_area;\n \n \/*\n * Read the width and height\n *\/\n r_area.read_input();\n \n \/*\n * Print the width and height\n *\/\n r_area.Rectangle::display();\n \n \/*\n * Print the area\n *\/\n r_area.display();\n \n return 0;\n}<\/pre>\n\n\n\n