This is lab 8: Question Design and write classes in a class hierarchy to describ
ID: 666354 • Letter: T
Question
This is lab 8:
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
Answer:
/* program using derived classes */
#include
#include
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:"< }
};
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:"< }
};
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();
}
I need help with the following:
Re-write your derived classes from Lab 8 to override a pure virtual function in the base class to compute the area of the shapes. Re-write the application from Lab 8 to create and store input shapes as base class pointers. In addition, modify the application to allow the user to create and store any number of shapes (use an STL class to store the base class pointers, i.e. vector shapes). After the user has finished creating shapes, compute and print the area for all the shapes in a single loop using only the base class pointers with dynamic binding.
Triangle:
area = ½ * base * height
Rectangle:
area = width * height
Circle:
area = PI * radius2
Explanation / Answer
#include <iostream.h>
#include <conio.h>
#define pi 3.142857
class shape
{
protected:
double x,y;
public:
virtualvoid get_data()=0;
virtualvoid display_area()=0;
};
class triangle : public shape
{
public:
void get_data(void)
{
cout<<" =====Data Entry for Triangle===== ";
cout<<"Enter base and height respectively : ";
cin>>x>>y;
}
void display_area(void)
{
cout<<" =====Area of Triangle===== ";
double aot;
aot = 0.5 * x * y;
cout<<"Area of Triangle is "<<aot;
}
};
class rectangle : public shape
{
public:
void get_data(void)
{
cout<<" =====Data Entry for Rectangle===== ";
cout<<"Enter length of two sides : ";
cin>>x>>y;
}
void display_area(void)
{
cout<<" =====Area of rectangle===== ";
double aor;
aor = x * y;
cout<<"Area of Rectangle is "<<aor;
}
};
class circle : public shape
{
public:
void get_data(void)
{
cout<<" =====Data Entry for circle===== ";
cout<<"Enter radius : ";
cin>>x;
}
void display_area(void)
{
cout<<" =====Area of Circle===== ";
double aoc;
aoc = (Pi*r)*r;
cout<<"Area of Circle is "<<aoc;
}
void main()
{
clrscr();
triangle tri;
rectangle rect;
circle cir
shape *list[3];
list[0]=&tri;
list[1]=▭
list[2]=○
int choice;
while(1)
{
clrscr();
cout<<" =====MEASURES OF DIFFERENT SHAPE===== ";
cout<<" Choose your choice ";
cout<<"1) Area of Triangle ";
cout<<"2) Area of Rectangle ";
cout<<"3) Area of Circle ";
cout<<"4) Exit ";
cout<<"Enter your choice:-";
cin>>choice;
switch(choice)
{
case 1 : list[0]->get_data();
list[0]->display_area();
getch();
break;
case 2 : list[1]->get_data();
list[1]->display_area();
getch();
break;
case 3 : list[2]->get_data();
list[1]->display_area();
getch();
break;
case 4 : goto end;
default: cout<<" Invalid choice Try again ";
getch();
}
}
end:
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.