Algorithm


Problem Name: Rectangle Area

Problem Link: https://www.hackerrank.com/challenges/rectangle-area/problem?isFullScreen=true

In this HackerRank Functions in C++ programming problem solution,

In this challenge, you are required to compute the area of a rectangle using classes.

 

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 (weight,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 <= weight,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

 

 

 

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
using namespace std;

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

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

int main(void)
{
    /*
     * 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;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
10 5

Output

x
+
cmd
10 5 50
Advertisements

Demonstration


Previous
[Solved] Inheritance Introduction in C++ solution in Hackerrank - Hacerrank solution C++
Next
[Solved] Multi Level Inheritance in C++ solution in Hackerrank - Hacerrank solution C++