每天打卡一小时 第二十三天
6-6 【CPP0028】以圆类Circle及立体图形类Solid为基础设计圆柱类Cylinder
分数 10
作者 C++多态编程
单位 石家庄铁道大学
以点类Point及平面图形类Plane为基类公有派生圆类Circle,再以圆类Circle及立体图形类Solid为基类公有派生圆柱类Cylinder,main(void)函数完成对圆柱类Cylinder的测试。
和昨天的圆锥类似
代码 复制,粘贴就好了 随便改改就对了 好简单
#define PI 3.14159 class Circle:public Point,public Plane { private: double radius; public: Circle(double a=0.0, double b=0.0, double c=0.0) { setX(a); setY(b); radius = c; cout<<"Circle Constructor run"<<endl; } Circle(const Circle &p):Point(p) { radius=p.radius; cout<<"Circle CopyConstructor run"<<endl; } ~Circle() { cout<<"Circle Destructor run"<<endl; } void setR(double r) { radius=r; } double getR()const { return radius; } void show()const { cout << "Circle(Point(" << "X=" << getX() << "," << "Y=" << getY() << ")," << "Radius=" << radius << ")"; } double area() const { return radius * PI * radius; } double length() const { return 2 * radius * PI; } }; class Cylinder:public Circle,public Solid { private: double height; public: Cylinder(double a=0.0, double b=0.0, double c=0.0, double d=0.0):Circle(a,b,c) { height=d; cout<<"Cylinder Constructor run"<<endl; } Cylinder(const Cylinder &p):Circle(p) { height=p.height; cout<<"Cylinder CopyConstructor run"<<endl; } ~Cylinder() { cout<<"Cylinder Destructor run"<<endl; } void show()const { cout<<"Cylinder(Circle(Point(X="<<getX()<<",Y="<<getY()<<"),Radius="<<getR()<<"),"<<"Height="<<height<<")"; } double s_Area()const { return getR()*getR()*PI*2+2*PI*getR()*height; } double volume()const { return getR()*getR()*PI*height; } void setH(double a) { height=a; } double getH() const { return height; } };
游刃有余