64,676
社区成员
发帖
与我相关
我的任务
分享
/*稍稍改进如下:*/
#include <iostream.h>
class circle
{
public:
circle( double = 0.0);
void setradius( double r ){ radius = r ;}
double getarea() const;
private:
double radius;
};
class table
{
public:
table( double = 0.0);
void setheight( double h ){ height = h ;}
double getheight() const;
private:
double height;
};
class roundtable :public circle, public table
{
public:
roundtable ( char = ' ',double = 0.0, double = 0.0 );
void settype( char c ){ type = c ;}
char gettype() const;
private:
char type;
};
circle::circle( double r )
{
radius= (r >= 0 ? r : 0);
}
double circle::getarea() const
{
return 3.14 * radius * radius ;
}
table::table( double h ) { height = ( h >= 0 ? h : 0 );}
double table::getheight() const { return height ; }
roundtable::roundtable( char c, double r, double h ):table( h ), circle( r )
{
type = c;
}
char roundtable::gettype() const { return type ; }
/*
设计一个圆类circle和一个桌子类table,另设计一个圆盘类roundtable,它是从前两个类的派生的,
要求输出一个圆桌的高度、面积和型号等数据。
说明:circle类包含double型私有数据成员radius和求圆面积的成员函数getarea();
table包含double型私有数据成员height和返回高度的成员函数getheight()。
roundtable类继承所有上述类的数据成员和成员函数。
添加了字符型私有数据成员type和相应的成员函数gettype()。需写出主函数进行测试。
求其详细代码!
*/
int main()
{
roundtable rd('t', 2,2);
cout<<"圆桌的高度:"<<rd.getheight()<<endl;
cout<<"圆桌的面积:"<<rd.getarea()<<endl;
cout<<"圆桌的型号:"<<rd.gettype()<<endl;
rd.setheight( 10 );
rd.setradius( 10 );
rd.settype( 'Y' );
cout<<"改变后的值如下:"<<endl;
cout<<"圆桌的高度:"<<rd.getheight()<<endl;
cout<<"圆桌的面积:"<<rd.getarea()<<endl;
cout<<"圆桌的型号:"<<rd.gettype()<<endl;
return 0;
}
#include <iostream.h>
class circle
{
public:
circle( double = 0.0);
double getarea() const;
private:
double radius;
};
class table
{
public:
table( double = 0.0);
double getheight() const;
private:
double height;
};
class roundtable :public circle, public table
{
public:
roundtable ( char = ' ',double = 0.0, double = 0.0 );
char gettype() const;
private:
char type;
};
circle::circle( double r )
{
radius= (r >= 0 ? r : 0);
}
double circle::getarea() const
{
return 3.14 * radius * radius ;
}
table::table( double h ) { height = ( h >= 0 ? h : 0 );}
double table::getheight() const { return height ; }
roundtable::roundtable( char c, double r, double h ):table( h ), circle( r )
{
type = c;
}
char roundtable::gettype() const { return type ; }
/*
设计一个圆类circle和一个桌子类table,另设计一个圆盘类roundtable,它是从前两个类的派生的,
要求输出一个圆桌的高度、面积和型号等数据。
说明:circle类包含double型私有数据成员radius和求圆面积的成员函数getarea();
table包含double型私有数据成员height和返回高度的成员函数getheight()。
roundtable类继承所有上述类的数据成员和成员函数。
添加了字符型私有数据成员type和相应的成员函数gettype()。需写出主函数进行测试。
求其详细代码!
*/
int main()
{
roundtable rd('t', 2,2);
cout<<"圆桌的高度:"<<rd.getheight()<<endl;
cout<<"圆桌的面积:"<<rd.getarea()<<endl;
cout<<"圆桌的型号:"<<rd.gettype()<<endl;
return 0;
}