Answer to Question #193313 in C++ for Sankalp

Question #193313

Create a base class called Shape. Use this class to store two double values that could be used to compute the area of figures. Derive three classes called as triangle, rectangle and circle from the base Shape. Add to the base class a member function get_data() to initialize base class data members and another member function display_area() to compute and display the area of figures. Make display_area() as a virtual function and redefine this function in the derived classes to suit their requirements. Using these four classes, design a program that will accept, dimensions of a triangle and rectangle and radius of circle, and display the area.The two values given as input will be treated as lengths of two sides in the case of rectangles and as base and height in the case of triangles and used as follows : Area of rectangle = x * y Area of triangle = ½ * x * y

1
Expert's answer
2021-05-14T04:20:48-0400
#include<iostream>
using namespace std;






class Shape
{
	public: double a,b;
		void get_data ()
		{
			cin>>a>>b;
		}
		virtual void display_area () = 0;
};


class Triangle:public Shape
{
	public: void display_area ()
	{
		cout<<"Area of triangle "<<0.5*a*b<<endl;
	}
};


class Rectangle:public Shape
{
	public: void display_area ()
	{
		cout<<"Area of rectangle "<<a*b<<endl;
	}
};




class Circle:public Shape
{
	public: void display_area ()
	{
		cout<<"Area of Circle "<<3.14*a*b<<endl;
	}
};








int main()
{	
	Triangle t;
	Shape *st = &t;
	cout<<"Enter base and altitude: ";
	st->get_data();
	st->display_area();
	
	
	Rectangle r;
	Shape *sr = &r;
	cout<<"Enter length and breadth: ";
	sr->get_data();
	sr->display_area();
	
	
Circle c;
	Shape *sc = &c;
	cout<<"Enter radius of circle two times(Note enter same radius both times ) : ";
	sc->get_data();
	sc->display_area();
	
	
	
	
	return 0;	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog