发布网友 发布时间:2022-04-24 07:30
共1个回答
热心网友 时间:2023-10-09 00:31
#include <iostream>
using namespace std;
#define PI 3.1415926
class Graphs2D
{
public:
//计算周长
virtual double CalcPerimeter() = 0;
//计算面积
virtual double CalcArea() = 0;
};
class Circle : public Graphs2D
{
public:
Circle(double radius):r(radius){}
public:
virtual double CalcPerimeter()
{
return 2 * r * PI;
}
virtual double CalcArea()
{
return PI * r * r;
}
private:
double r;
};
class Rect : public Graphs2D
{
public:
Rect(double height,double width):h(height),w(width){}
public:
virtual double CalcPerimeter()
{
return 2 * (h + w);
}
virtual double CalcArea()
{
return h * w;
}
private:
double h;
double w;
};
class Graphs3D
{
public:
//计算表面积
virtual double CalcSurfaceArea() = 0;
//计算体积
virtual double CalcVolume() = 0;
};
class Cylinder : public Graphs3D
{
public:
Cylinder(double radius,double height):r(radius),h(height){}
public:
virtual double CalcSurfaceArea()
{
return 2 * PI * r * r + 2 * PI * r * h;
}
virtual double CalcVolume()
{
return PI * r * r * h;
}
private:
double r;
double h;
};
int main()
{
Graphs2D* pCircle = new Circle(3.0);
Graphs2D* pRect = new Rect(5.0,6.0);
Graphs3D* pCylinder = new Cylinder(2.0,5.0);
cout << "圆的周长为:" << pCircle->CalcPerimeter() << ",面积为:" << pCircle->CalcArea() << endl;
cout << "矩形的周长为:" << pRect->CalcPerimeter() << ",面积为:" << pRect->CalcArea() << endl;
cout << "圆柱体的表面积为:" << pCylinder->CalcSurfaceArea() << ",体积为:" << pCylinder->CalcVolume() << endl;
return 0;
}
追问
不好意思 必须要用到这些 我忘记发了