Design and write classes in a class hierarchy to describe different types of geo
ID: 666032 • Letter: D
Question
Design and write classes in a class hierarchy to describe different types of geometric shapes. Provide information in the classes in order to compute the area of each of the shapes. Create three separate classes (Triangle,Rectangle,Circle) that are derived from a separate base class (Shape). Write a program that allows a user to create any of the different shape types. Prompt the user to select the shape type and enter the necessary dimensions (i.e. refer to formulas below; rectangle needs width and height, circle needs radius, triangle needs height and base). After the user has finished creating the shape, compute and print out the area.
Use the following formulas for your program:
Triangle:
area = ½ * base * height
Rectangle:
area = width * height
Circle:
area = PI * radius2
Triangle:
area = ½ * base * height
Rectangle:
area = width * height
Circle:
area = PI * radius2
Explanation / Answer
/* program using derived classes */
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
doubel x,y;
public:
void get_data();
void dispaly_area();
};
class triagle : public shape
{
public:
void get_data()
{
cout<<" Enter data for a Triangle";
cout<<" Enter Base and Height :";
cin>>x>>y;
}
void display_area()
{
cout<<" Area of Triangle";
double area;
area = 0.5 * x * y;
cout<<"Area of a triangle:"<<area;
}
};
class rectangle : public shape
{
public:
void get_data()
{
cout<<" Enter data for a Rectangle";
cout<<" Enter width and height";
cin>>x>>y;
}
void display_area()
{
cout<<" Area of Rectangle";
double area;
area = x * y;
cout<<"Area of a rectangle:"<<area;
}
};
void main()
{
triangle tri;
rectangle rect;
clrscr();
cout<<" Measures of Different Shapes:";
cout<<" 1. Area of a Triangle";
cout<<" 2. Area of a Rectangle";
cout<<" 3. Exit";
cout<<" Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
tri.get_data();
tri.display_area();
break;
case 2:
rect.get_data();
rect.display_area();
break;
case 3:
break;
default:
cout<<"Enter the correct choice";
break;
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.