电工电子技术实验 2022-06-23 11:37 采纳率: 97.4%
浏览 23
已结题

vc++相关程序问题

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

img

  • 写回答

2条回答 默认 最新

  • 关注

    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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月23日
  • 已采纳回答 6月23日
  • 创建了问题 6月23日

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了