ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:218.89KB ,
资源ID:6739503      下载积分:1 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-6739503.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(08092C++2试题B含答案16.docx)为本站会员(b****4)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

08092C++2试题B含答案16.docx

1、08092C+2试题B含答案16姓名 学号 学院 专业 座位号 ( 密 封 线 内 不 答 题 )密封线线_ _ 诚信应考,考试作弊将带来严重后果! 华南理工大学期末考试 2009-6-26高级语言程序设计(2)试卷B注意事项:1. 考前请将密封线内各项信息填写清楚; 2. 所有答案写在答题纸上,答在其它地方无效; 3. 试卷可做草稿纸,试卷必须与答题纸同时提交;4考试形式:闭卷; 5. 本试卷共五大题,满分100分,考试时间120分钟。一.单项选择题(每题2分,共20分)1. 下列有关C+构造函数,析构函数的说法正确的是( B )A系统提供默认的析构函数,不提供默认的构造函数B构造函数可以重

2、载,析构函数不可以重载C构造函数可以提供默认参数,也可以提供返回值D析构函数不可以定义为虚函数2. 下列有关C+的this指针的说法正确的是( C )A定义一个类后,this指针就指向该类B调用静态成员函数会隐含传递this指针参数C友元函数没有this指针Dthis指针是隐含的,不能显式使用3. 只能用友元函数重载的运算符是( A )ACBase:print() D ptr-print()7. 如果CInterface是抽象类,下列语句错误的是(A)ACInterface obj; B CInteface* getInterface(); CCInterface *ptr; Dchar* g

3、etName(CInterface&);8. 针对如下代码段,正确的说法是(D)Class CInterface public: virtual const char* getID()const =0; /*/ ACInterface是抽象类 BgetID函数是纯虚函数CgetID函数没有执行代码 DA,B,C说法都正确 9. 对如下类模板错误使用是(A)template class Tany /*/ ;ACArray any;BCArray IntanyCdouble get(const Tany);Dtemplate T get(const Tany* );10. 用于处理字符串流的是(A

4、)Astrstream Bios Cfstream Diosteam二阅读下列程序,写出执行结果(每题6分,共24分)1. / 输入输出流#include#include#includevoid main() char ch=a; ofstream outfile; outfile.open(f.txt); if(!outfile) coutf.txt cannot open!endl; abort(); while(ch=f) outfilech) coutchar(ch+3) ; coutendl; 答案: d e f g h i 2/虚函数、继承性与多态性#includeclass Ba

5、se public: virtual void f() coutBaseendl; ;class SubClass:public Base public: virtual void f() coutSubClassendl; ;void test(Base & b) b.f(); void main() Base bc; SubClass sc; test(bc); test(sc);答案: Base SubClass3. /模板#includeconst int NUM=4;template void traverse(T a) for( int i=0; iNUM; i+ ) cout a

6、i; coutendl;templatevoid bubbleSort(T a) for( int i=0; iNUM; i+ ) for( int j=i+1; jaj ) swap(a,i,j);template void swap( T a, int i, int k ) T temp; temp=ai; ai=ak; ak=temp; void main() int aNUM = 4, 3, 2, 1 ; double bNUM = 4.4, 3.3, 2.2, 1.1 ; bubbleSort(a); traverse(a); bubbleSort(b); traverse(b);答

7、案: 1 2 3 4 1.1 2.2 3.3 4.4 4. /浅复制、深复制与自定义复制构造函数。#include#includeclass Name public: Name(char *pn); Name(const Name &Obj); Name(); protected: char *pName; int size;Name:Name(char *pn) coutConstruct pnendl; pName=new charstrlen(pn)+1; if(pName!=0) strcpy(pName,pn); size=strlen(pn);Name:Name(const Nam

8、e &Obj) coutCopy Obj.pName into its own blockn; pName= new charstrlen(Obj.pName)+1; if(pName!=0) strcpy(pName,Obj.pName); size=Obj.size;Name:Name() coutDestruct pNameendl; pName0=0; delete pName; pName = NULL; size=0;void main() Name Obj1(anne); Name Obj2=Obj1;答案: construct anne copy anne into ints

9、own block destruct anne destruct anne三.简答题(每小题4分,共20分)1有以下程序:#include class X private: int a; double y; / ;void main() coutsizeof(X)endl; 执行结果如图1所示,这是否说明X:a和X:y占有存储空间?类的数据成员是什么时候建立存储空间的?举例说明,并请写一个初始化类X数据成员的函数。 不是。创建对象时分配存储空间。 例如:在main 函数中有X Xobj; 在类定义中加入 public : X( int a1= 0, double y1 = 0 ) a=a1;

10、y=y1; ,执行时创建对象Xobj,分配存储空间X.a, X.y。2有以下程序,类B是类A的派生类。你认为该程序能够等价地定义为类B包含类A吗?如果可以,请你写出类B的定义。#include class A int a; public: A( int aa ) a=aa; ;class B : public A int b; public : B(int aa, int bb ):A(aa) b=bb; void out() coutatbendl; ;void main() B x(1,2); x.out(); class B int b; A ba; public : B( int aa

11、, int bb ):ba( aa ) b=bb; void out() coutba.atbendl; ;3有程序:#include class A protected: int a; public: A(int aa)a=aa; void print() couta=aendl; ;class X: public A protected: double y; public: X(int aa, double yy) : A(aa) y=yy; void print() couta=aty=yprint(); 输出结果如图2所示。p-print()为什么不能输出X类对象的全部数据成员?请你给

12、出两种修改程序的方法,可以调用X:print()输出数据12和3.1425。 (1)定义虚函数virtual void print(); (2)用X类指针建立对象X *p=new X(12,3.1425); (3)做指针强类型转换 (X*)p)-print();4若有说明: template void fun( T , T ); int a, double x;以下语句 fun( a, a ); fun( x, x ); fun( a, x );有错误吗?若出错,请分析原因,并给出解决方案。 重载 void fun(int double);5以下程序不能正确显示文件中的数据。请分析原因,并修改

13、相应代码。#include void main() ofstream fout( d:DATA.dat, ios:out|ios:binary ) ; int i,j ; for( i=0; ij; coutj ; fin.close();ifstream fin( d:DATA.dat, ios:in|ios:binary ) ;for( i=0; i20; i+ ) fin.read( (char*)&j, sizeof(int) ); coutjends; fin.close(); 四. 根据程序输出填空。(每空2分,共24分) 1. 程序运行效果如图3。/成员和友员#include c

14、lass BBank;class CBank int balance;public :CBank(int b=0) balance=b; void getbalance() ; void disp()cout The savings in China Bank is: balance endl; (1) total(CBank, BBank) ; / friend void ; (2) getbalance() /void CBank: cout balance; class BBank int balance;public:BBank( int b = 0 ) balance= b; voi

15、d getbalance() coutbalance; void disp() coutThe savings in Business Bank is:balance endl; (3) void total(CBank, BBank) ; /friend ;void total( CBank A, BBank B ) cout Total savings is: A.balance+B.balance endl;void main() CBank X(100); BBank Y; X.disp () ; Y.disp () ; (4) ; / Y.getbalance () total (

16、X, Y) ;2. 程序运行效果如图4所示。重载运算符#include class CRect int length, width; public : CRect () ; CRect(int l, int w) length=l; width=w; void disp() cout The area is: length*width endl;CRect& (5) ( CRect r ) / operator= length=r.length; width=r.width; return (6) ; / *this CRect operator + (CRect r) CRect temp;

17、 (7) /temp.length=r.length+length;temp.width=r.width+width; return temp; ;void main() CRect r1(3,3) , r2(5,8) , r3(2,4) ; r1.disp(); r2.disp(); r3.disp() ;r1=r2=r2+r3 ; r1.disp(); 3. 程序运行效果如图5所示。/虚继承#includeclass Base public: Base(int i)coutBase constructor called iendl; ;class Base1: (8) Base /virt

18、ual public public: Base1(int i,int j): Base(j) coutBase1 constructor called iendl; ;class Base2: (9) /public Base public: Base2(int i,int j): Base(j) coutBase2 constructor called iendl; ;class Derived: public Base2, public Base1 public: Derived(int i, int a,int b,int c): (10) Base1(i,a), Base2(i,b),

19、 Base(c) coutDerived constructor called iendl; ;int main() Derived objD( 1, 2, 3, 4); return 0; 4. 程序运行效果如图6所示。/静态数据#includeclass counter (10) ; /static int num int t; public : counter(int a) t=a; void show()const cout t t num endl; ; (10) = 5; /int counter:num void main () counter a(1), b(2), c(3)

20、; couta: ; a.show() ; coutb: ; b.show() ; coutc: ; c.show() ;5. 程序运行效果如图7所示。/继承#include #include class TwoDShape / 二维图形类 double width, height; public: TwoDShape( (11) ) width = w; height = h; / double w=0, double h=0 void showDim()coutWidth and height are width and height n; double getWidth() return

21、 width; double getHeight() return height; void setWidth(double w) width = w; void setHeight(double h) height = h; ; class Triangle : public TwoDShape char style20; public: Triangle (12) strcpy(style, str); / ( char *str, double w=0, double h=0 ) : TwoDShape(w, h) double area() return getWidth() * ge

22、tHeight() / 2; void showStyle() cout Triangle is style n; ; int main() Triangle ct1( right, 7.0, 12.0 ), ct2( right) ; ct1.showStyle(); ct1.showDim(); cout Area is ct1.area() endl; ct2.showStyle(); ct2.showDim(); cout Area is ct2.area() endl; return 0; 五、完成程序。(第1小题4分,第2小题8分,共12分)1根据程序输出(如图8所示),以最小形式

23、补充A类的成员函数。#includeclass A int a,b,c; /成员函数;void main () A a1(11,22,33); A a2(a1); couta1: ; a1.out(); couta2: ; a2.out();public: A(int aa, int bb, int cc) coutInitial objectn; a=aa; b=bb; c=cc; A(A&obj) coutCopy initial objectn; a=obj.a; b=obj.b; c=obj.c; void out()couta b cendl; 2本程序包含了time类和Date类的声明。请设计一个Birthtime类,它继承了time类和Date类,并且还有一项学生的名字Studentname。并设计主函数显示一个学生的名字和出生日期、时间。输出效果如图9所示。#include#includeclass Time publi

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2