文档详情

C++期末复习(程序分析).doc

发布:2017-12-08约1.4万字共20页下载文档
文本预览下载声明
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 T class Tany { T x,y; public: Tany(T a,T b){x=a,y=b;} T sum( ){return x+y;} }; void main() { Tany (int) obj(10,100); } 答:Tany(int) obj(10,100); 出错,应为 Tanyint 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 前加声明语句 class two ; ) 。 4、指出下面程序段中的错误,并说明出错原因。 # include iostream.h 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() 有二义性。 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 的定义中有一处错误,请用下横线标出错误所在行并说明错误原因。 # includeiostream.h # includestring.h class A { public: A(const char *nm){strcpy(name,nm);} private: char name[80]; }; 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 函数中的错误所在行,并说明错误原因。 # includeiostream.h 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
显示全部
相似文档