zoukankan      html  css  js  c++  java
  • 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;
    }

     运行结果:

  • 相关阅读:
    Maria 与Ann的故事
    引语
    Preface
    Chapter 1 Foundation
    Roman to Integer
    Integer to Roman
    Container with most water
    palindrome number
    String to Integer (atoi)
    Reverse Integer
  • 原文地址:https://www.cnblogs.com/duanqibo/p/14348777.html
Copyright © 2011-2022 走看看