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;
}

 运行结果:

 

posted @ 2021-01-30 13:17  bobo哥  阅读(124)  评论(0编辑  收藏  举报