欢迎来到天天文库
浏览记录
ID:47426716
大小:91.00 KB
页数:10页
时间:2020-01-10
《c++编程题》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、4.1测试一个名为rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。解:源程序:#includeclassRectangle{public:Rectangle(inttop,intleft,intbottom,intright);~Rectangle(){}intGetTop()const{returnitsTop;}intGetLeft()const{returnitsLeft;}intGetBottom()const{returnitsBottom;}i
2、ntGetRight()const{returnitsRight;}voidSetTop(inttop){itsTop=top;}voidSetLeft(intleft){itsLeft=left;}voidSetBottom(intbottom){itsBottom=bottom;}voidSetRight(intright){itsRight=right;}intGetArea()const;private:intitsTop;intitsLeft;intitsBottom;intitsRight;};Rectan
3、gle::Rectangle(inttop,intleft,intbottom,intright){itsTop=top;itsLeft=left;itsBottom=bottom;itsRight=right;}intRectangle::GetArea()const{intWidth=itsRight-itsLeft;intHeight=itsTop-itsBottom;return(Width*Height);}intmain(){RectangleMyRectangle(100,20,50,80);intAre
4、a=MyRectangle.GetArea();cout<<"Area:"<using namespace std; class Box {public: float L; float getBMJ(){return L*L*6;} float getTJ(){return
5、L*L;} Box(float in){L=in;} }; void main() { Box r(10); cout<<"边长:10表面积:"<6、#includeclassvehicle //汽车类,包含车轮数和车重{ public: vehicle(int,float); intget_wheels(); floatget_weight(); voidshow(); protected: intwheels; //车轮数 floatweight; //车重量,单位吨};classcar:privatevehicle //小车类是汽车类的私有派生类,包含载客数{ public: car(i7、ntwheels,floatweight,intpassengers); intget_passengers(); voidshow(); private: intpassenger_load; //额定载客数};classtruck:privatevehicle //卡车类是汽车类的私有派生类,包含载人数和载重量{ public: truck(intwheels,floatweight,intpassengers,floatmax_load); intget_passengers(); voidsho8、w(); private: intpassenger_load; //额定载人数 floatpayload; //额定载重量,单位吨};vehicle::vehicle(intwl,floatwt){ wheels=wl; weight=wt;}intvehicle::get_wheels(){ returnwheels
6、#includeclassvehicle //汽车类,包含车轮数和车重{ public: vehicle(int,float); intget_wheels(); floatget_weight(); voidshow(); protected: intwheels; //车轮数 floatweight; //车重量,单位吨};classcar:privatevehicle //小车类是汽车类的私有派生类,包含载客数{ public: car(i
7、ntwheels,floatweight,intpassengers); intget_passengers(); voidshow(); private: intpassenger_load; //额定载客数};classtruck:privatevehicle //卡车类是汽车类的私有派生类,包含载人数和载重量{ public: truck(intwheels,floatweight,intpassengers,floatmax_load); intget_passengers(); voidsho
8、w(); private: intpassenger_load; //额定载人数 floatpayload; //额定载重量,单位吨};vehicle::vehicle(intwl,floatwt){ wheels=wl; weight=wt;}intvehicle::get_wheels(){ returnwheels
此文档下载收益归作者所有