一个C++的机试题不会做,求其详细代码
一个C++的机试题不会做,请教大家!求其详细代码!
设计一个圆类circle和一个桌子类table,另设计一个圆盘类roundtable,它是从前两个类的派生的,要求输出一个圆桌的高度、面积和型号等数据。说明:circle类包含double型私有数据成员radius和求圆面积的成员函数getarea();table包含double型私有数据成员height和返回高度的成员函数getheight()。roundtable类继承所有上述类的数据成员和成员函数。添加了字符型私有数据成员type和相应的成员函数gettype()。需写出主函数进行测试。
求其详细代码!
------解决方案--------------------
#define PI 3.1415
class circle
{
public:
circle():radius(0){};
void setradius(double r){ radius = r;}
const double getarea(){ return PI*radius*radius; }
private:
double radius;
};
class table
{
public:
table():height(0){};
void setheight(int h){ height = h; }
const int getheight(){ return height; }
private:
int height;
};
class roundtable: public circle,public table
{
public:
roundtable():type(0){};
int type;
void settype(int t){ type = t; }
const int gettype(){ return type; }
};
void main()
{
roundtable tt;
tt.setradius(5.0);
tt.setheight(10);
tt.settype(50);
cout << tt.getarea() << endl;
cout << tt.getheight() << endl;
cout << tt.gettype() << endl;
}
------解决方案--------------------
#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;
}
------解决方案--------------------
设计一个圆类circle和一个桌子类table,另设计一个圆盘类roundtable,它是从前两个类的派生的,要求输出一个圆桌的高度、面积和型号等数据。说明:circle类包含double型私有数据成员radius和求圆面积的成员函数getarea();table包含double型私有数据成员height和返回高度的成员函数getheight()。roundtable类继承所有上述类的数据成员和成员函数。添加了字符型私有数据成员type和相应的成员函数gettype()。需写出主函数进行测试。
求其详细代码!
------解决方案--------------------
#define PI 3.1415
class circle
{
public:
circle():radius(0){};
void setradius(double r){ radius = r;}
const double getarea(){ return PI*radius*radius; }
private:
double radius;
};
class table
{
public:
table():height(0){};
void setheight(int h){ height = h; }
const int getheight(){ return height; }
private:
int height;
};
class roundtable: public circle,public table
{
public:
roundtable():type(0){};
int type;
void settype(int t){ type = t; }
const int gettype(){ return type; }
};
void main()
{
roundtable tt;
tt.setradius(5.0);
tt.setheight(10);
tt.settype(50);
cout << tt.getarea() << endl;
cout << tt.getheight() << endl;
cout << tt.gettype() << endl;
}
------解决方案--------------------
#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;
}
------解决方案--------------------
- C/C++ code
/*稍稍改进如下:*/ #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; }