行业知识
关于纯虚函数---=0要写
�index.php/tags-41973.html" >�
首先定义一个抽象类Shape,它包括计算体积的纯虚函数calVolume()和显示结果的纯虚函数display()。同时,有立方体、圆柱、圆锥、球这4个类继承该抽象类,并重写calVolume()方法和display()方法。在主函数中,创建这4个类的对象,并用父类的引用或指针指向它们,然后调用calVolume()方法和display()方法,从而实现动态多态。具体要求如下:
| Shape(抽象类) |
| + calVolume():void // 计算体积 + display():void // 显示结果 |
| Cube(立方体) |
| - length:double //长 - width: double //宽 - height: double //高 - volume: double //体积 |
| + Cube(double length,double width,double height) + calVolume():void // 计算体积 + display(): void // 显示结果 |
| Cylinder(圆柱) |
| - radius:double //底面半径 - height: double //高 - volume: double //体积 |
| + Cylinder(double radius,double height) + calVolume():void // 计算体积 + display(): void // 显示结果 |
| Cone(圆锥) |
| - radius:double //底面半径 - height: double //高 - volume: double //体积 |
| + Cone(double radius,double height) + calVolume():void // 计算体积 + display(): void // 显示结果 |
| Sphere(球体) |
| - radius:double //半径 - volume: double //体积 |
| + Sphere(double radius) + calVolume():void // 计算体积 + display(): void // 显示结果 |
//#include #include #include using namespace std;class Shape {public:virtual void calVolume () = 0;virtual void display() const = 0;};class Cube :public Shape {public:Cube(double leng = 0.5, double wid = 0.5, double heig = 0.5);void calVolume();void display() const;~Cube() {}private:double length;double width;double height;double volume;};class Cylinder :public Shape {public:Cylinder(double r = 0.5, double h = 0.5);void calVolume();void display() const;~Cylinder() {}private:double radius;double height;double volume;};class Cone :public Shape {public:Cone(double r = 0.5, double h = 0.5);void calVolume();void display() const;~Cone() {}private:double radius;double height;double volume;};class Sphere :public Shape {public:Sphere(double r = 0.5);void calVolume();void display() const;~Sphere() {}private:double radius;double volume;};//Cube::Cube(double leng, double wid, double heig) :height(heig), width(wid), length(leng) {}void Cube::calVolume() {this->volume = this->height * this->width * this->length;cout
上一篇:怎么让服务器崩塌?