问题:设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生出来的,要求输出一个圆桌的高度、面积和颜色等数据。
程序代码:
#include<iostream>
using namespace std;
class circle
{ public:
circle(double i):x(i) {}
void GetArea() {cout<<3.14*x*x<<endl;}
private:
double x;
};
class table
{ public:
table(double h):height(h) {}
void display1() {cout<<"h="<<height<<endl;}
private:
double height;
};
class roundtable:public circle,public table
{ public:
roundtable(char c[],double i,double h):circle(i),table(h) {color=new char[strlen(c)+1];strcpy(color,c);}
void display2()
{
cout<<color<<endl;
}
private:
char *color;
};
int main()
{ roundtable a("blue",2,1);
cout<<"高度:";
a.display2();
cout<<"面积:";
a.GetArea();
cout<<"高度";
a.display1();
return 0;
}
执行结果: