c++编程题(整理)(一).pdf

  1. 1、本文档共10页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
4.1 测试一个名为rectangle 的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。 解: 源程序: #include <iostream.h> class Rectangle { public: Rectangle (int top, int left, int bottom, int right); ~Rectangle () {} int GetTop() const { return itsTop; } int GetLeft() const { return itsLeft; } int GetBottom() const { return itsBottom; } int GetRight() const { return itsRight; } void SetTop(int top) { itsTop = top; } void SetLeft (int left) { itsLeft = left; } void SetBottom (int bottom) { itsBottom = bottom; } void SetRight (int right) { itsRight = right; } int GetArea() const; private: int itsTop; int itsLeft; int itsBottom; int itsRight; }; Rectangle::Rectangle(int top, int left, int bottom, int right) { itsTop = top; itsLeft = left; itsBottom = bottom; itsRight = right; } int Rectangle::GetArea() const { int Width = itsRight-itsLeft; int Height = itsTop - itsBottom; return (Width * Height); } int main() 1 { Rectangle MyRectangle (100, 20, 50, 80 ); int Area = MyRectangle.GetArea(); cout << "Area: " << Area << "\n"; return 0; } 程序运行输出: Area: 3000 Upper Left X Coordinate: 20 4.2 设计一个程序。 设计一个立方体类Box ,它能计算并输出立方体的体积和表面积。 #include <iostream> using namespace std; class Box {public: float L; float getBMJ(){return L*L*6;} float getTJ(){return L*L;} Box(float in){L=in;} }; void main() { Box r(10); cout<<"边长:10\n 表面积:"<<r.getBMJ()<<"\n 体积:"<<r.getTJ(); } 4.3 设计一个汽车类vehicle,包含的数据成员有车轮个数wheels 和车重weight 。小车类car 是它的派生类,其中包含载人数passenger_load 。每个 类都有相关数据的输出方法。在主程序中定义一个car 类对象,对其车轮个数、车重、载人数进行设置并显示。 #include <iostream.h> #include <iomanip.h> class vehicle //汽车类,包含车轮数和车重 { public: vehicle(int, float); int get_wheels(); float get_weight(); void show(); protected: int wheels; //车轮数 float weight; //车重量,单位吨 }; class car:private vehicle //小车类是汽车类的私有派生类,包含载客数 { public:

文档评论(0)

万寿无疆 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档