07/03/2023

Rectangle Area Hackerrank Solution in C++ | Hackerrank Sol.

HackerRank/rectangle-area.cpp or Rectangle Area Leaderboard or Rectangle Area Hackerrank Solution in C++. In this challenge, you are required to compute the area of a rectangle using classes. The Rectangle Area should be handled within the Rectangle class, not by implementing a class extension as required by the exercise. Rectanglearea. Create two classes:

Rectangle Area HackerRank Solution In C++

Rectangle

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

RectangleArea

The RectangleArea class is derived from the Rectangle class, i.e., it is the sub-class of the Rectangle class. The class should have the read_input() method, to read the values of the 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.

Submit this solution: Click Here

Rectangle Area HackerRank Solution In C++


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

Rectangle Area HackerRank Solution Output


Rectangle Area HackerRank Solution Output

Similar to the Rectangle Area


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: