Define abstract base class Shape, derive from it five derived classes: Circle (Circle), Square (Square), Rectangle (Rectangle), Trapezoid (Trapezoid) and Triangle (Triangle), use virtual function to calculate the area of each figure, output their area and. An array of base Pointers is required, with each array element pointing to an object of a derived class. PI=3.14159f, single-precision floating-point calculation.

Input format:

Enter 9 numbers greater than 0 in a single line, separated by Spaces, representing the radius of a circle, the sides of a square, the width and height of a rectangle, the top, bottom and height of a trapezoid, and the bottom and height of a triangle.

Output format:

Prints the area sum of all graphs, reserving 3 significant digits after the decimal point.

Example Input:

12.6 3.5 4.5 8.4 2.0 4.5 3.2 4.5 8.4 No blank line at the endCopy the code

Example output:

578.109 No blank line at the endCopy the code

Code:

#include <iostream>
#include <iomanip>
using namespace std;
#definePI 3.14159 f
 
class Shape
{
	public:
		virtual float Area(a) = 0;
};
 
class Circle:public Shape
{
	float r;
	public:
		Circle(float _r):r(_r) {}
		virtual float Area(a)
		{
			returnPI*r*r; }};class Square:public Shape
{
	float t;
	public:
		Square(float _t) :t(_t) {}
		virtual float Area(a)
		{
			returnt*t; }};class Rectangle:public Shape
{
	float x, y;
	public:
		Rectangle(float _x, float _y):x(_x), y(_y) {}
		virtual float Area(a)
		{
			returnx*y; }};class Trapezoid:public Shape
{
	float s1, s2, h;
	public:
		Trapezoid(float _s1, float _s2, float _h):s1(_s1), s2(_s2), h(_h) {}
		virtual float Area(a)
		{
			return (s1+s2)*h/2; }};class Triangle:public Shape
{
	float a, b;
	public:
		Triangle(float _a, float _b):a(_a), b(_b) {}
		virtual float Area(a)
		{
			return a*b/2; }};int main(a)
{
	float r, t, x, y, s1, s2, h, a, b;
	cin >> r >> t >> x >> y >> s1 >> s2 >> h >> a >> b;
	Circle c(r);
	Square s(t);
	Rectangle re(x, y);
	Trapezoid z(s1, s2, h);
	Triangle tri(a, b);
	Shape *pt[5] = {&c,&s,&re,&z,&tri};
	double area = 0.0;
	for(int i=0; i<5; i++) area += pt[i]->Area(a); cout << fixed <<setprecision(3) << area << endl;
	return 0;
}
Copy the code

Submission Results: