Hackerrank Rectangle Area Solution
1 min read

Hackerrank Rectangle Area Solution

Hackerrank Rectangle Area Solution

.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

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  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

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,  and , so

Solution in cpp

Approach 1.

class Rectangle{    
    public:
    int width, height;
    void Display(){
        cout<<width<<" "<<height<<endl;
    }
};
class RectangleArea: public Rectangle{
    public:
    void Input(){
        cin>>width>>height;
    }
    void Display(){
        cout<<width*height;
    }
};

Approach 2.


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

};
class RectangleArea: public Rectangle {
    public: void Input() { 
        cin>>width>>length; 
    } 
    void Display() { 
        int c; 
        c=width*length; 
        cout << c;
    }
};

Approach 3.

class Rectangle{
    public:
        int width,height;
        void Display(){
            printf("%d %d\n",width,height);
        }
};
class RectangleArea : public Rectangle{
    public:
        void Input(){
            scanf("%d %d",&width,&height);
        }
        void Display(){
            printf("%d\n",width*height);
        } 
};

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×