欢迎来到天天文库
浏览记录
ID:47612673
大小:92.51 KB
页数:20页
时间:2019-10-07
《C++期末复习(程序分析)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C++期末复习(程序分析题)一、程序改错题1、指出下面程序段中的错误,并说明出错原因。classA{inta,b;public:A(intaa,intbb){a=aa;b=bb;}};voidmain(){Ax(2,3),y(4);}答:Ax(2,3),y(4);语句出错,因为没有单参数的构造函数(或者y(4)少写了一个参数)。2、指出并改正下面利用类模板的对象定义中的错误。templateclassTany{Tx,y;public:Tany(Ta,Tb){x=a,y=b;}Tsum(){returnx+y;}};voidmain(){Tany(int)obj(1
2、0,100);}答:Tany(int)obj(10,100);出错,应为Tanyobj(10,100);语句。3、指出下面程序段中的错误,并说明出错原因。classone{private:inta;public:voidfunc(two&);};classtwo{private:intb;friendvoidone::func(two&);};voidone::func(two&r){a=r.b;}答:voidfunc(two&);出错,two尚未声明(应在classone前加声明语句classtwo;)。4、指出下面程序段中的错误,并说明出错原因。#include3、ostream.h>classA{public:voidfun(){cout<<″a.fun″<4、ation{intX,Y=20;protected:intzeroX,zeroY;intSetZero(intZeroX,intZeroY);private:intlength,height;public:floatradius;voidinit(intinitX,intinitY);intGetX();intGetY();};答:intX,Y=20;出错,不能采用这种方式初始化。6、下面的程序类B的定义中有一处错误,请用下横线标出错误所在行并说明错误原因。#include#includeclassA{public:A(constcha5、r*nm){strcpy(name,nm);}private:charname[80];};classB:publicA{public:B(constchar*nm):A(nm){}voidPrintName()const;};voidB::PrintName()const{cout<<"name:"<classLocation{priva6、te:intX,Y;public:voidinit(intinitX,intinitY);intsumXY();};voidLocation::init(intinitX,intinitY){X=initX;Y=initY;}intLocation::sumXY(){returnX+Y;}voidmain(){LocationA1;intx,y;A1.init(5,3);x=A1.X;y=A1.Y;cout<clas7、sTest{public:staticintx;};intx=20;//对类的静态成员初始化错误voidmain(){cout<
3、ostream.h>classA{public:voidfun(){cout<<″a.fun″<4、ation{intX,Y=20;protected:intzeroX,zeroY;intSetZero(intZeroX,intZeroY);private:intlength,height;public:floatradius;voidinit(intinitX,intinitY);intGetX();intGetY();};答:intX,Y=20;出错,不能采用这种方式初始化。6、下面的程序类B的定义中有一处错误,请用下横线标出错误所在行并说明错误原因。#include#includeclassA{public:A(constcha5、r*nm){strcpy(name,nm);}private:charname[80];};classB:publicA{public:B(constchar*nm):A(nm){}voidPrintName()const;};voidB::PrintName()const{cout<<"name:"<classLocation{priva6、te:intX,Y;public:voidinit(intinitX,intinitY);intsumXY();};voidLocation::init(intinitX,intinitY){X=initX;Y=initY;}intLocation::sumXY(){returnX+Y;}voidmain(){LocationA1;intx,y;A1.init(5,3);x=A1.X;y=A1.Y;cout<clas7、sTest{public:staticintx;};intx=20;//对类的静态成员初始化错误voidmain(){cout<
4、ation{intX,Y=20;protected:intzeroX,zeroY;intSetZero(intZeroX,intZeroY);private:intlength,height;public:floatradius;voidinit(intinitX,intinitY);intGetX();intGetY();};答:intX,Y=20;出错,不能采用这种方式初始化。6、下面的程序类B的定义中有一处错误,请用下横线标出错误所在行并说明错误原因。#include#includeclassA{public:A(constcha
5、r*nm){strcpy(name,nm);}private:charname[80];};classB:publicA{public:B(constchar*nm):A(nm){}voidPrintName()const;};voidB::PrintName()const{cout<<"name:"<classLocation{priva
6、te:intX,Y;public:voidinit(intinitX,intinitY);intsumXY();};voidLocation::init(intinitX,intinitY){X=initX;Y=initY;}intLocation::sumXY(){returnX+Y;}voidmain(){LocationA1;intx,y;A1.init(5,3);x=A1.X;y=A1.Y;cout<clas
7、sTest{public:staticintx;};intx=20;//对类的静态成员初始化错误voidmain(){cout<
此文档下载收益归作者所有