Develop a C++ program of the givenUML Class diagram and program description below. Program Description:An application that would compute and display the total area which is the summation of all the areas of the three given shapes (Pentagon, Square, Triangle). The sides of the pentagon, square and triangle are all equal.
#include <iostream>
using namespace std;
class Figure {
private:
double side;
public:
Figure(double s) : side(s) {};
double getSide() { return side; };
virtual double area() = 0;
virtual const char* getName() = 0;
};
class Pentagon : public Figure {
public:
Pentagon(double s) : Figure(s) {};
double area() { return 1.72047740059 * getSide() * getSide(); };
const char* getName() { return "pentagon"; };
};
class Square : public Figure {
public:
Square(double s) : Figure(s) {};
double area() { return getSide() * getSide(); };
const char* getName() { return "square"; };
};
class Triangle : public Figure {
public:
Triangle(double s) : Figure(s) {};
double area() { return getSide() * getSide() * sqrt(3.0)/4; }
const char* getName() { return "triangle"; };
};
int main()
{
Figure* arr[3];
double s = 0;
arr[0] = &Pentagon(4.0);
arr[1] = &Square(4.0);
arr[2] = &Triangle(4.0);
for (Figure* f : arr) {
s += f->area();
cout << "The area of the " << f->getName() << " is " << f->area() << endl;
}
cout << "The sum of the areas of the three figures is " << s << endl; ;
}
Comments
Leave a comment