C++程序设计(高起专).doc

  1. 1、本文档共162页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
考前练兵-试题详解 应用题(6) 案例分析题(6) 简答题(5) (1) 请在横线处填上适当的字句,以使程序完整。 #include iostream.h #include ″math.h″ class Point { private: double X,Y; ①____?______Line; public: Point(double x=0, double y=0) { X=x; Y=y; } Point(Point p) { X=p.X; Y=p.Y; } }; class Line { private: Point p1,p2; public: Line(Point xp1, Point xp2): ②___?_______{} double GetLength(); }; double Line::GetLength() { double dx=p2.X-p1.X; double dy=p2.Y-p1.Y; return sqrt(dx*dx + dy*dy); } void main() { Point p1,p2(3,4); Line L1(p1,p2); coutL1.GetLength()endl; } 正确答案: (1) friend class (2) p1(xp1),p2(xp2) (2) 请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为5。 #includeiostream.h class Integer { int x; public: Integer(int a=0){x=a;} void display( ){coutxendl;} ①?; }; Integer Max(Integer a,Integer b) { if(②?) return a; return b; } void main( ) { Integer a(3),b(5),c; c=Max(a,b); c.display( ); } 正确答案: (1) friend Integer Max(Integer a,Integer b); (2) a.xb.x (3) 设计一个三角形类Triangle,包含三角形三条边长的私有数据成员,另有一个重载运算符“+”,以实现求两个三角形对象的面积之和。 正确答案: 参考程序: #includeiostream.h #includemath.h class Triangle { int x,y,z; double area; public: Triangle(int i,int j,int k) { double s; x=i;y=j;z=k; s=(x+y+z)/2.0; area=sqrt(s*(s-x)*(s-y)*(s-z)); } void disparea() { coutArea=areaendl; } friend double operator+(Triangle t1,Triangle t2) { return t1.area+t2.area; } }; void main() { Triangle t1(3,4,5),t2(4,5,6); double s; coutt1:; t1.disparea(); coutt2:; t2.disparea(); s=t1+t2; cout总面积=sendl; } (4) 下面程序中A是抽象类,为使其输出是: This is class B printing. This is class C printing. 请在横线处填写适当内容,以使程序完整。 #include iostream.h class A{ public: ①____?____; }; class B: public A{ public: void printMe() {cout This is class B printing. endl;} }; class C: public B { void printMe() {cout This is class C printing. endl;} }; void print (②__?______) { a.printMe(); } void main () { B b; C c; print (b); print (c); } 正确答案: (1)virtual void printMe()=0 (2)A a (5) 设计一个复数类Complex,包含实部real和虚部image两个私有数据成员,另有一个重载运算符“+”(用成员函数实现),以实现求两个复数对象之和,和一个重载运算符“”,以实现输出复数类的对象。在主程序中用实例进行测试。 正确答案: 参考程序: ?????? #includeiostre

文档评论(0)

亲近阳光 + 关注
实名认证
内容提供者

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

1亿VIP精品文档

相关文档