Shapes and Virtual Functions Please help me with this program written in C++. Ma
ID: 654190 • Letter: S
Question
Shapes and Virtual Functions
Please help me with this program written in C++. Many thanks ahead of time!
Here is a link to the assigment description:
https://www.dropbox.com/s/xz3e9h7ld0xwnnw/Exercise%205%20Description.pdf?dl=0
Shapes and Virtual Functions
Overview
In this project you are building a base class, with pure virtual functions. The class is called GeometricShape. You will then create the classes Circle, Rectangle and Triangle. These will all inherit publicly from GeometricShape will implement the virtual functions. You can inline the member functions for trivial member functions, but you must NOT inline all of the member functions in a class.
Building the base class GeometricShape
In the base class GeometricShape you need to have a virtual destructor and three pure virtual functions. The following are the signatures of the virtual destructor and the three pure virtual functions:
virtual ~GeometricShape();
virtual double getArea() const = 0;
virtual double getPerimeter() const = 0;
virtual std::string to_string() const = 0;
Outside of the GeometricShape class definition, but in the header file, you need to create the following function:
std::ostream& operator<< (std::ostream& outputStream,
const GeometricShape& shape);
In the operator<< function you will call the GeometricShape function to_string() to actually build the formatted output. You will be implementing this to_string member function in each of the classes that is derived from the GeometricShape class.
You will need a GeometricShape.h file and a GeometricShape.cpp file for this class.
Building the Circle class
The Circle class will be implemented with files Circle.h and Circle.cpp. The Circle class will inherit, publicly, from GeometricShape. You need to implement a destructor and the three virtual functions getArea, getPerimeter and to_string. You will also need a constructor that takes one input parameter, the radius of the circle. The radius is of type double. The getArea function needs to calculate the area and return it back as a double. The getPerimeter function needs to calculate the perimeter (circumference) of the circle.
For the to_string function you need the following:
std::string Circle::to_string() const
{
return "Circle radius(" + std::to_string(radius) + ")";
}
Note that the std::to_string(radius) function is part of the string class and you need to include .
Building the Rectangle class
The Rectangle class will be implemented with files Rectangle.h and Rectangle.cpp. It will have a destructor and will implement the three virtual functions. It will also have a constructor. The constructor will take two input parameters, both of type double. The first parameter is the width and the second is the length. When you implement the to_string function you need to display that it is a Rectangle and you need to display the width and the length. This is similar to what you did for the Circle class
Explanation / Answer
#include <vector>
#include <iostream>
using namespace std;
#define PI 3.14159
char ch;
main()
{
clrscr();
textcolor(4);
intro();
getch();
textcolor(7);
clrscr();
do
{
ch=menu();
switch(ch)
{
case 'a':
case 'A':
clrscr();
square();
getch();
break;
case 'b':
case 'B':
clrscr();
rect();
getch();
break;
case 'c':
case 'C':
clrscr();
circl();
getch();
break;
case 'd':
case 'D':
clrscr();
tri();
getch();
break;
case 'e':
case 'E':
clrscr();
rom();
getch();
break;
case 'f':
case 'F':
clrscr();
para();
getch();
break;
case 'g':
case 'G':
clrscr();
tra();
getch();
break;
case 'h':
case 'H':
clrscr();
qua();
getch();
break;
case 'i':
case 'I':
clrscr();
semicir();
getch();
break;
case 'j':
case 'J':
clrscr();
msector();
getch();
break;
case 'k':
case 'K':
clrscr();
sphere();
getch();
break;
case 'l':
case 'L':
clrscr();
cone();
getch();
break;
case 'm':
case 'M':
clrscr();
cyll();
getch();
break;
case 'n':
case 'N':
clrscr();
cube();
getch();
break;
case 'o':
case 'O':
clrscr();
cuboid();
getch();
break;
case 'p':
case 'P':
clrscr();
hemisphe();
getch();
break;
case 'q':
case 'Q':
exit(1);
}
} while(ch!='Q'||ch!='q');
getch();
}
intro()
{
int i;
clrscr();
printf("
");
textcolor(2);
cprintf("********************************************************************************************");
textcolor(4);
printf("
PROGRAM TO CALCULATE AREAS , VOLUMES ,
CIRCUMFERENCES OF SHAPES ");
printf("
=====================================================
");
printf("
OF VARIOUS GEOMETRIC SHAPES");
printf("
===========================
");
textcolor(2);
cprintf("********************************************************************************************");
getch();
}
menu()
{
clrscr();
textcolor(7);
printf(" OPTION MENU
Two Dimensional Shapes.
-----------------------
A.SQUARE
B.RECTANGLE
C.CIRCLE
D.TRIANGLE
E.RHOMBUS
F.PARALLELOGRAM
G.TRAPEZIUM
H.QUADRILATERAL.
I.SEMICERCLE
J.SECTOR
");
printf("
Three Dimensional Shapes Menu.
-------------------------
K.SPHERE
L.CONE
M.CYLLINDER
N.CUBE
O.CUBOID
P.HEMISPHERE
Q.QUIT
Enter Your Choice :");
scanf("%c",&ch);
return(ch);
}
square()
{
float s,a,p;int i,j;
printf("
Enter side of square:");
scanf("%f",&s);
a=s*s;
p=4*s;
printf("
Perimeter of square : %.3f units",p);
printf("
Area of square : %.3f sq.units",a);
printf("
Square is ...
");
for(i=1;i<=s;i++)
{
textcolor(10);
for(j=1;j<=s;j++)
cprintf("
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.