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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

本文(C++期末复习程序分析.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++期末复习程序分析.docx

1、C+期末复习程序分析C+期末复习(程序分析题) 一、程序改错题 1、指出下面程序段中的错误,并说明出错原因。 class A int a,b; public : A(int aa,int bb) a=aa;b=bb; ; void main() A x(2,3),y(4); 答:Ax(2,3),y(4); 语句出错,因为没有单参数的构造函数 ( 或者 y(4) 少写了一个参数 ) 。2、指出并改正下面利用类模板的对象定义中的错误。 template class Tany T x,y; public: Tany(T a,T b)x=a,y=b; T sum( )return x+y; ; voi

2、d main() Tany (int) obj(10,100); 答:Tany(int) obj(10,100); 出错,应为 Tany obj(10,100); 语句。 3、 指出下面程序段中的错误,并说明出错原因。 class one private: int a; public: void func(two&); ; class two private: int b; friend void one:func(two&); ; void one:func(two& r) a=r.b; 答:void func(two&); 出错, two 尚未声明 (应在 class one 前加声明语句

3、 class two ; ) 。 4、指出下面程序段中的错误,并说明出错原因。 include class A public: void fun( )cout a.fun endl; ; class B public:void fun( )cout b.fun endl; void gun( )cout b.gun endl; ; class C:public A,public B private:int b; public:void gun( ) cout c.gun endl; void hun( ) fun( ); ; 答:void hun()fun(); 出错, C : fun() 有

4、二义性。5、指出下面程序段中的错误,并说明出错原因。 class Location int X,Y=20; protected: int zeroX,zeroY; int SetZero(int ZeroX,int ZeroY); private: int length,height; public: float radius; void init(int initX,int initY); int GetX( ); int GetY( ); ;答:int X,Y=20; 出错,不能采用这种方式初始化。6、下面的程序类 B 的定义中有一处错误,请用下横线标出错误所在行并说明错误原因。 # in

5、clude # include class A public: A(const char *nm)strcpy(name,nm); private: char name80; ; class B:public A public: B(const char *nm):A(nm) void PrintName( )const; ; void B:PrintName( )const cout name: nameendl; /错误,name为私有成员 void main( ) B b1( wang li ); b1.PrintName( ); 7、用下横线标出下面程序 main 函数中的错误所在行,

6、并说明错误原因。 # include class Location private: int X,Y; public: void init(int initX,int initY); int sumXY( ); ; void Location:init(int initX,int initY) X=initX; Y=initY; int Location:sumXY( ) return X+Y; void main( ) Location A1; int x,y; A1.init(5,3); x=A1.X;y=A1.Y; coutx+y A1.sumXY( )endl; /错误 8、下面的程序

7、有一处错误,请用下横线标出错误所在行并改正错误。# include class Test public: static int x; ; int x=20; / 对类的静态成员初始化错误 void main ( ) coutTest:x; 9、指出下面的程序中的两处错误,并改正class Exampublic: Exam(int y=10) data=y; int getdata() const return +data; /错误常成员函数不能修改对象/修改方法:删除关键字const static int getcount() coutdata; / 错误静态成员函数不能直接访问非静态成员/修

8、改方法:删除cout这一行 return +count ; private: int data; static int count; ;10、指出下面的程序中的错误,并改正 char *string; string=new char free(string);/用new动态分配的内存不能用free函数来释放/修改方法:用delete运算符来释放11、指出下面的程序中的错误#include #include class Person public: Person(char* pN) cout Constructing pN endl; pName=new charstrlen(pN)+1; if

9、(pName!=0) strcpy(pName,pN); Person() cout Destructing pName endl; pName0=0; delete pName; protected: char* pName;void main() Person p1(Randy); Person p2=p1; /即Person p2(p1);/对同一个空间的两次释放,修改方法:增加一个复制构造函数Person:Person(Person& p) pName=new charstrlen(p.pName)+1; strcpy(pName,p.pName);五、写出下面程序的执行结果 1、#

10、include class A int * a; public: A (int x) a = new int(x); cout*a = *aendl; delete a; ; void main() A x(3), *p; p = new A (5); delete p; 输出为:*a=3*a=52、# include template void f (T &x, Q &y) if (sizeof (T) sizeof (Q) x = (T)y; else y = (Q)x; void main() double d; int i; d = 9999; i = 88; f(d,i); cout

11、 d= d i= i endl; d = 88; i = 9999; f(i,d); cout d= d i= i endl; 输出为: d=88 i=88d=9999,i=99993、# include class base public: virtual int func () return 0; ; class derived: public base public: int func() return 100; ; void main() derived d; base & b = d; cout b.func() endl; cout b.base:func() endl; 输出为:

12、 100 04、# include class Test private: static int val; int a; public: static int func(); static void sfunc(Test &r); ; int Test:val = 20; int Test:func() val += val; return val; void Test:sfunc(Test &r) r.a = 25; cout Result3 = r.aendl; void main() cout Result1 = Test:func() endl; Test a; cout Result

13、2 = a.func()endl; Test:sfunc(a); 输出为: Result1 =40 Result2=80Result3=255、#include template class Tclass private: T x,y; public: Tclass(T a,T b):x(a)y=b; Tclass(T a)y=(T)0,x=a; void pr( ) char c; c=(y=(T)0 ? +:-); cout x c (T)0? y:-y) iendl; ; void main( ) Tclass a(10.5,-5.8); a.pr( ); Tclass b(10); b

14、.pr( ); 输出为: 10.5-4.8i 10+0i6、#include class test public: test() cout 调用构造函数endl; ;void f(int i) static test x; cout iendl; void main() f(10); f(20);输出为: 调用构造函数 10207、include void main( ) int *a; int *&p=a; int b=10; p=&b; cout*a; 输出为:108、 include template Tf(T*a,T*b,int n) Ts=(T)0; for(int i=0;in;i

15、+) s+=a i *b i ; return s; void main() double c 5 =1.1,2.2,3.3,4.4,5.5,d 5 =10.0,100.0,1000.0; coutf(c,d,5)endl; 输出为:3531 9、#include void main() for(int i=0;i4;i+) coutendlsetfill( )setw(4-i) 0 setfill(#)setw(i+i)0?0: ); 输出为: 0 0#0 0#0 0#0 10、运行下面的程序,写出当输入 25 , 60 时的输出结果。 #include class goods privat

16、e: static int totalWeight; int weight; public: goods(int w) weight=w; totalWeight+=w; goods(goods& gd) weight=gd.weight; totalWeight+=weight; goods() totalWeight-=weight; int getwg() return weight; static int getTotal() return totalWeight; ; int goods:totalWeight=0; void main() int w; cout The initi

17、al weight of goods:goods:getTotal()w; / 输入 25 goods g1(w); cinw; / 输入 60 goods g2(w); cout The total weight of goods: goods:getTotal()endl; 输出为: The initial weight of goods:0 The total weight of goods:8511、# include class A public: virtual void print( )cout This is class A printing. endl; ; class B:

18、public A public: void print( ) ; class C:public B public: void print( )cout This is class C printing. endl; ; void show(A&a) a.print( ); void main( ) A a; B b; C c; show(a); show(b); show(c); 输出为: This is class A printing. This is class C printing.12、# include class A public: A( )a=0; A(int i)a=i; v

19、oid Print( )couta , ; int Geta( )return a; private: int a; ; class B:public A public: B( )b=0; B(int i,int j,int k); void Print( ); private: int b; A aa; ; B:B(int i,int j,int k):A(i),aa(j)b=k; void B:Print( ) A:Print( ); coutb , aa.Geta( )endl; void main( ) B bb2; bb0=B(1,2,5); bb1=B(3,4,7); for(in

20、t i=0;i2;i+) bbi.Print( ); 输出为: 1, 5, 2 3, 7, 413、#include class Test private: static int val; int a; public: static int func( ); void sfunc(Test &r); ; int Test:val=200; int Test:func( ) return val+; void Test:sfunc(Test &r) r.a=125; cout Result3= r.aendl; void main( ) cout Result1= Test:func( )end

21、l; Test a; cout Result2= a.func( )endl; a.sfunc(a); 输出为: Result1 =200 Result2=201Result3=12514、# include class Location int X,Y; public: void init(int=0,int=0); void valueX(int val)X=val; int valueX( )return X; void valueY(int val)Y=val; int valueY( )return Y; ; void Location:init(int initX,int init

22、Y) X=initX; Y=initY; void main( ) Location A,B; A.init( ); coutA.valueX( ) A.valueY( )endl; A.valueX(5); coutA.valueX( ) A.valueY( )endl; B.init(6,2); B.valueY(4); coutB.valueX( ) B.valueY( )endl; 输出为: 0 0506 415、#include class A public: A( ) virtual void func( )coutDestructor A endl; A( ) func(); ;

23、 class B:public A public: B( ) void func()coutDestructor B endl; B( ) func(); ; void main( ) B b; A&a=b; 输出为:Destructor B Destructor A16、 include class My Class public: int number; void set(int i); ; int number=3; void MyClass:set (int i) number=i; void main() MyClass my1; int number=10; my1.set(5);

24、 coutmy1.numberend1; my1.set(number); coutmy1.numberendl; my1.set(:number); coutmy1.number; 输出为:5 10 3 17、#include class AAA int A,B; public: AAA(int i,int j) A=i,B=j; coutCn; AAA()coutDn; void print( ) coutA,Bprint(); a2-print(); delete a1; delete a2; 输出为:CC1,25,6 DD 18、#include class b1 public: b1(int x) coutxAn; b1() coutBn; ; class b2 public: b2(int x) coutxCn; b2() coutDn; ; class AAA:public b2,public b1 public:

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1