Answer to Question #158847 in C++ for ken

Question #158847

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.


1
Expert's answer
2021-01-27T04:16:22-0500
#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; ;
}

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