设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生的,要求输出一个圆桌的高度、面积和颜色等数据。(每个类都要定义带参的构造函数)
#include
using namespace std;
class Circle
{
public:
Circle(double radius)
{
r=radius;
}
double area()
{
s=3.14*r*r;
return s;
}
private:
double r,s;
};
class Table
{
public:
Table(double h)
{
height=h;
}
double GetH()
{
return height;
}
private:
double height;
};
class Roundtable:public Circle,public Table
{
public:
Roundtable(double radius,double h, char *c)
:Circle(radius),Table(h)
{
color=new char[strlen(c)+1];
strcpy(color,c);
}
string GetC()
{
return color;
}
private:
char *color;
};
int main()
{
Roundtable A(3.6,0.9,"RED");
cout<<"the Basic information of the roundtable : "<<endl;
cout<<"面积 : "<<A.area()<<endl;
cout<<"高度 : "<<A.GetH()<<endl;
cout<<"颜色 : "<<A.GetC()<<endl;
}
这个代码和下面那个代码有啥差别吗?
#include
using namespace std;
class Circle
{
public:
Circle(double radius)
{
r=radius;
}
double area()
{
s=3.14*r*r;
return s;
}
private:
double r,s;
};
class Table
{
public:
Table(double h)
{
height=h;
}
double GetH()
{
return height;
}
private:
double height;
};
class Roundtable:public Circle,public Table
{
public:
Roundtable(double radius,double h, string c)
:Circle(radius),Table(h)
{
color=c;
}
string GetC()
{
return color;
}
private:
string color
};
int main()
{
Roundtable A(3.6,0.9,"RED");
cout<<"the Basic information of the roundtable : "<<endl;
cout<<"面积 : "<<A.area()<<endl;
cout<<"高度 : "<<A.GetH()<<endl;
cout<<"颜色 : "<<A.GetC()<<endl;
}