为了账号安全,请及时绑定邮箱和手机立即绑定

求C++大神帮我解析一下下面的这个程序。

#include<iostream>

#include<string>

using namespace std;

class Shape

{

public:

virtual double GetX()const=0;

virtual double GetY()const=0;

virtual string GetName()=0;

virtual double Area() const{return 0;};

virtual double Volume() const{return 0;};

protected:

double x_;

double y_;

double volume_;

double area_;

};


class Point:public Shape

{

public:

Point(double x,double y){x_=x;y_=y;}

virtual double GetX()const

{return x_;}

virtual double GetY()const

{return y_;}

string GetName()

{return "点";}

};


class Rectangle:public Point

{

public:

Rectangle(double x,double y):Point(x,y){x_=x;y_=y;}

virtual double Area() const

{return (x_*y_);}

string GetName()

{return "矩形";}

};


class Ellipsp:public Rectangle

{

public:

Ellipsp(double x,double y):Rectangle(x,y){x_=x;y_=y;}

virtual double Area() const

{return 3.14*(x_/2)*(y_/2);}

string GetName()

{return "椭圆";}

};


class Circle:public Ellipsp

{

public:

double r;

Circle(double x):Ellipsp(x,x){r=x;}

virtual double Area() const

{return 3.14*r*r;}

string GetName()

{return "圆";}

};


class Cylinder:public Circle

{

public:

Cylinder(double x,double y):Circle(x){x_=x;y_=y;}

virtual double Area() const

{return 2*3.14*x_*y_;}

virtual double Volume() const

{return 3.14*x_*x_*y_;}

string GetName()

{return "圆柱";}

};


int main(void)

{

Shape *shape[5];

shape[0]=new Point(3,5);

shape[1]=new Rectangle(3,5);

shape[2]=new Ellipsp(3,5);

shape[3]=new Circle(3);

shape[4]=new Cylinder(4,6);


cout<<"序号"<<'\t'<<"名称"<<'\t'<<"X"<<'\t'<<"Y"<<'\t'<<"面积"<<'\t'<<"体积"<<endl;

for(int i=0;i<5;i++)

cout<<i+1<<'\t'<<shape[i]->GetName()<<'\t'<<shape[i]->GetX()<<'\t'<<shape[i]->GetY()<<'\t'<<shape[i]->Area()<<'\t'<<shape[i]->Volume()<<endl;

for(int i=0;i<5;i++)

delete shape[i];

return 0;

}


正在回答

举报

0/150
提交
取消
C++远征之模板篇
  • 参与学习       91156    人
  • 解答问题       318    个

本C++教程力求即学即会,所有知识以实践方式讲解到操作层面

进入课程

求C++大神帮我解析一下下面的这个程序。

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信