C++这道题应该怎么做?
circle类包含私有数据成员radius和求圆面积的成员函数getarea();table类包含私有数据成员height和返回高度的成员函数getheight()。roundtable类继承所有上述类的数据成员和成员函数,添加了私有数据成员color和相应的成员函数。 展开
#include<iostream>
using namespace std;
class circle
{
public:
circle(double r=0):radius(r)
{
}
double getarea() const
{
return Pi*radius*radius;
}
private:
static const double Pi=3.14159;
double radius;
};
class table
{
public:
table(double h):height(h)
{
}
double getheight() const
{
return height;
}
private:
double height;
};
class roundtable : public circle,public table
{
public:
roundtable(double r=0,double h=0,string c="棕色"):circle(r),table(h),color(c)
{
}
string getcolor() const
{
return color;
}
void setcolor(string c)
{
color=c;
}
private:
string color;
};
int main()
{
roundtable rt(1.6,1.2,"金红");
cout<<"圆桌高:"<<rt.getheight()<<"米"<<endl;
cout<<"圆桌面积:"<<rt.getarea()<<"平方米"<<endl;
cout<<"圆桌颜色:"<<rt.getcolor()<<endl;
return 0;
}