2/25/2018

Rectangle Area HackerRank Solution In C++

Problem:- HackerRank/rectangle-area.cpp or Rectangle Area Leaderboard or Rectangle Area solutions or Rectangle area hackerRank solution in c++ or rectangle area solutions hackerRank or rectangle area solutions in c++ or hackerRank rectangle area solutions in c++ or hackerRank solutions c++.

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

Here, width = 10 and height = 5, so area = width * height = 50.

Copy the colored code and paste into the editor.

Submit this solution:- Click Here

Solution:-

#include <iostream>
using namespace std;

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

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

Output:-


Rectangle Area Solutions HackerRank

You May Also See

1. Hacker Rank Solution For Birthday Cake Candles

2. C++ Program For Store Employee Information And Display Using Structure

3. C Program For Find A Grade Of Given Marks Using Switch Case

4. C Program For HEAP Sort In Ascending Order Also Show Complexity

5. C++ Program For Selection Sort Using Function

1 comment:

  1. Marvelous and fascinating article. Incredible things you've generally imparted to us. Much obliged. Simply keep making this kind out of the post.
    C plus plus Courses in Chennai

    ReplyDelete