• c++多重继承


    设计一个圆类circle和一个桌子类table。circle类包含私有数据成员radius和求圆面积的成员函数getarea();table类包含私有数据成员height和返回高度的成员函数getheight()。roundtable类继承所有上述类的数据成员和成员函数,添加了私有数据成员color和相应的成员函数。其中,main函数已给出。请完成程序的其他部分。

    源程序:

    #include <iostream>
    #include <string>
    using namespace std;
    class circle //圆类
    {
    private:
    double radius;
    public:
    circle(double r)
    {
    radius=r;
    }
    double getarea()
    {
    return radius*radius*3.14;
    }
    };
    class table //桌子类
    {
    private:
    double height;
    public:
    table(double h)
    {
    height=h;
    }
    double getheight()
    {
    return height;
    }
    };
    class roundtable:public table,public circle //圆桌类,继承了上面两个类
    {
    char *color1;
    public:
    roundtable(double h,double r,char c[]):circle(r),table(h)
    {
    color1=new char[strlen(c)+1];
    strcpy(color1,c);

    }
    char *getcolor()
    {
    return color1;
    }
    };

    void main()
    {
    roundtable rt(0.8,1.2,"黑色");
    cout<<"圆桌属性数据:"<<endl;
    cout<<"高度:"<<rt.getheight()<<endl;
    cout<<"面积:"<<rt.getarea()<<endl;
    cout<<"颜色:"<<rt.getcolor()<<endl;
    }

     运行结果:

  • 相关阅读:
    AtCoder Grand Contest 013 C:Ants on a Circle
    AtCoder Grand Contest 010 C:Cleaning
    055 列表推导式
    054 三元表达式
    05 Python爬虫之信息标记与提取方法
    053 迭代器
    052 装饰器
    051 闭包函数
    04 Python爬虫之Beautiful Soup库
    03 Python爬虫之Requests网络爬取实战
  • 原文地址:https://www.cnblogs.com/duanqibo/p/14348777.html
Copyright © 2020-2023  润新知