vc++相关程序问题

vc++相关程序问题 以及 其多态性实践相关问题……谢谢了……利用虚函数……

img

img

代码如下:

#include <iostream>
using namespace std;

class Shape
{
protected:
    float x, y;
    float vol;
public:
    virtual void disp() { vol = 0; }
    virtual void print() { cout << "体积为" << vol << endl; }
};

class Zhengfangti :public Shape
{

public:
    Zhengfangti(float v) { x = v; }
    void disp() { vol = x * x * x; }
    void print() { cout <<"边长为"<<x << "的正方体的体积:" << vol<<endl; }
};

class Circle :public Shape
{
public:
    Circle(float r) { x = r; }
    void disp() { vol = 4.0 / 3 * 3.14 * x * x * x; }
    void print() { cout << "半径为" << x << "的球的体积为:" << vol<<endl; }
};

class Yuanzhuti :public Shape
{
public:
    Yuanzhuti(float r, float h) { x = r, y = h; }
    void disp() { vol = 3.14*x*x*y; }
    void print() { cout << "半径为" << x << "的圆柱体的体积为:" << vol<<endl; }
};

int main()
{
    Zhengfangti z(2);
    z.disp();
    z.print();
    Circle c(3);
    c.disp();
    c.print();
    Yuanzhuti y(2, 3);
    y.disp();
    y.print();
    return 0;
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632