Rectangle Area in C++ - Hacker Rank Solution


Rectangle Area in C++ - Hacker Rank Solution


Problem

Create two classes:

Rectangle
The Rectangle class should have two data fields-width and height of int types. The class should have display() method, to print the width and height of the rectangle separated by space.

RectangleArea
The RectangleArea class is derived from Rectangle class, i.e., it is the sub-class of Rectangle class. The class should have read_input() method, to read the values of width and height of the rectangle. The RectangleArea class should also overload the display() method to print the area (width * height) of the rectangle.



Input Format :

The first and only line of input contains two space separated integers denoting the width and height of the rectangle.

Constraints :

1 <= width, height <= 100

Output Format :

The output should consist of exactly two lines:
In the first line, print the width and height of the rectangle separated by space.
In the second line, print the area of the rectangle.



Sample Input :

10 5

Sample Output :

10 5
50

Explanation :

as width = 10 and height = 5 , so area = width * height = 50



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//Rectangle Area in C++ - Hacker Rank Solution
#include <iostream>

using namespace std;
/*
 * Create classes Rectangle and RectangleArea
 */
 
/* Rectangle Area in C++ - Hacker Rank Solution START */

class Rectangle 
{
    public:
    int width, height;
    void display() 
    {
        cout << width << " " << height << "\n";
    }
};

class RectangleArea : public Rectangle 
{
    public:
    void read_input() 
    {
        cin >> width;
        cin >> height;
    }
    void display() 
    {
        cout << width * height << "\n";
    }
};

/* Rectangle Area in C++ - Hacker Rank Solution END */

int main()
{
    /*
     * Declare a RectangleArea object
     */
    RectangleArea r_area;
    
    /*
     * Read the width and height
     */
    r_area.read_input();
    
    /*
     * Print the width and height
     */
    r_area.Rectangle::display();
    
    /*
     * Print the area
     */
    r_area.display();
    
    return 0;
}





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
1 Comments
  • Unknown
    Unknown Saturday, November 28, 2020

    thanks:)

Add Comment
comment url