创建Shape类层次结构
1.创建基类Shape
class Shape
{
public:
virtual double getArea(){}//计算面积
virtual double getVolume(){}//计算体积
virtual void print(){}//打印
Shape(){}//构造
~Shape(){}//析构
};
2.创建TwoDimensionalShape类,继承于Shape
class TwoDimensionalShape : public Shape//二维
{
public:
TwoDimensionalShape(){}
virtual double getArea(){}
virtual double getVolume(){}
virtual void print(){}
};
3.创建ThreeDimensionalShape类,继承于Shape类
class ThreeDimensionalShape : public Shape//三维
{
public:
ThreeDimensionalShape(){}
virtual double getArea(){}
virtual double getVolume(){}
virtual void print(){}
};
4.创建圆形类Circle,正方形类Square,三角形类Triangle,继承于二维类TwoDimensionalShape
class Circle : public TwoDimensionalShape//二维的圆形
{
public:
Circle(){}
Circle(double radius)
{
radius_m=radius;
}
double getArea()
{
return E*radius_m*radius_m;
}
double getVolume(){}
void print()
{
cout<<"二维的圆形的面积为:"<<getArea()<<endl;
}
private:
double radius_m;//圆的半径
};
class Square : public TwoDimensionalShape//二维的正方形
{
public:
Square(){}
Square(double length,double width)
{
length_m=length;
width_m=width;
}
double getArea()
{
return length_m*width_m;
}
double getVolume(){}
void print()
{
cout<<"二维的正方形的面积为:"<<getArea()<<endl;
}
private:
double length_m;//长
double width_m;//宽
};
class Triangle : public TwoDimensionalShape//二维的三角形
{
public:
Triangle(){}
Triangle(double length,double height)
{
length_m=lengt