Dream追逐 2016-04-28 04:30 采纳率: 100%
浏览 1281
已采纳

C++程序,公有继承之后成员的值变成了随机值,怎么回事儿

#include
#include
using namespace std;

const float Pi = 3.1415;

class C {
public:
C(float r, float h) :Radius(r), high(h) {}
protected:
float Radius;
float high;
};

class Round :public C {
public:
void supArea();
void volume();
};

void Round::supArea() {
float s;
s = 4 * Pi*pow(C::Radius, 2);
cout << "\n球的表面积为:" << s << endl;
}

void Round::volume() {
float v;
v = 4 * Pi*pow(C::Radius, 3) / 3;
cout << "\n球的体积为:" << v << endl;
}

class Cylinder :public C {
public:
void supArea();
void volume();
};

void Cylinder::supArea() {
float s;
s = 2 * Pi*pow(C::Radius, 2) + C::high * 2 * Pi*C::Radius;
cout << "\n圆柱的表面积为:" << s << endl;
}

void Cylinder::volume() {
float v;
v = Pi*pow(C::Radius, 2)*C::high;
cout << "\n圆柱的体积为:" << v << endl;
}

class Cone :public C {
public:
void supArea();
void volume();
};

void Cone::supArea() {
float s;
s = Pi*pow(C::Radius, 2) + Pi*C::Radius*C::high;
cout << "\n圆锥的表面积为:" << s << endl;
}

void Cone::volume() {
float v;
v = 1 / 3 * Pi*pow(C::Radius, 2)*sqrt(pow(C::high, 2) - pow(C::Radius, 2));
cout << "\n圆锥的体积为:" << v << endl;
}

int main() {
C q(1, 2);
Round r;
r.supArea();
r.volume();
Cylinder c1;
c1.supArea();
c1.volume();
Cone c2;
c2.supArea();
c2.volume();
return 0;
}

  • 写回答

2条回答 默认 最新

  • sueastward 2016-04-28 06:04
    关注

    #include
    using namespace std;
    const float Pi = 3.1415;
    class C {
    public:
    C(float r, float h) :Radius(r), high(h) {}
    protected:
    float Radius;
    float high;
    };
    class Round :public C {
    public:
    Round(float r,float h):C(r,h){}
    void supArea();
    void volume();
    };
    void Round::supArea() {
    float s;

    s = 4 * Pi*pow(C::Radius, 2);
    cout << "\n球的表面积为:" << s << endl;
    

    }
    void Round::volume() {
    float v;
    v = 4 * Pi*pow(C::Radius, 3) / 3;
    cout << "\n球的体积为:" << v << endl;
    }
    class Cylinder :public C {
    public:
    Cylinder(float r,float h):C(r,h){}
    void supArea();
    void volume();
    };
    void Cylinder::supArea() {
    float s;
    s = 2 * Pi*pow(C::Radius, 2) + C::high * 2 * Pi*C::Radius;
    cout << "\n圆柱的表面积为:" << s << endl;
    }
    void Cylinder::volume() {
    float v;
    v = Pi*pow(C::Radius, 2)*C::high;
    cout << "\n圆柱的体积为:" << v << endl;
    }
    class Cone :public C {
    public:
    Cone(float r,float h):C(r,h){}
    void supArea();
    void volume();
    };
    void Cone::supArea() {
    float s;
    s = Pi*pow(C::Radius, 2) + Pi*C::Radius*C::high;
    cout << "\n圆锥的表面积为:" << s << endl;
    }
    void Cone::volume() {
    float v;
    v = 1 / 3 * Pi*pow(C::Radius, 2)*sqrt(pow(C::high, 2) - pow(C::Radius, 2));
    cout << "\n圆锥的体积为:" << v << endl;
    }
    int main() {

    Round r(1,2);
    r.supArea();
    r.volume();
    Cylinder c1(1,2);
    c1.supArea();
    c1.volume();
    Cone c2(1,2);
    c2.supArea();
    c2.volume();
    
    return 0;
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?