c++期末复习(程序分析)资料

上传人:E**** 文档编号:107074545 上传时间:2019-10-17 格式:DOC 页数:21 大小:92.50KB
返回 下载 相关 举报
c++期末复习(程序分析)资料_第1页
第1页 / 共21页
c++期末复习(程序分析)资料_第2页
第2页 / 共21页
c++期末复习(程序分析)资料_第3页
第3页 / 共21页
c++期末复习(程序分析)资料_第4页
第4页 / 共21页
c++期末复习(程序分析)资料_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《c++期末复习(程序分析)资料》由会员分享,可在线阅读,更多相关《c++期末复习(程序分析)资料(21页珍藏版)》请在金锄头文库上搜索。

1、C+期末复习(程序分析题) 一、程序改错题 1、指出下面程序段中的错误,并说明出错原因。 class Aint 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; ; void main()Tany

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

3、。 4、指出下面程序段中的错误,并说明出错原因。 include class Apublic: 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 Bprivate:int b; public:void gun( ) cout c.gun endl; void hun( ) fun( ); ; 答:void hun()fun(); 出错, C : fun() 有二义性。5、指出下面程序段中的错误

4、,并说明出错原因。 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 的定义中有一处错误,请用下横线标出错误所在行并说明错误原因。 # include # include c

5、lass Apublic:A(const char *nm)strcpy(name,nm); private:char name80; ; class B:public Apublic: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 函数中的错误所在行,并说明错误原因。 # include clas

6、s Locationprivate: 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; /错误常成员函数不能修改对象/修改方法:删除关键字conststatic int getcount()coutdata;/ 错误静态成员函数不能直接访问非静态成员/修改方法:删除cout这一行return +count ;

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

9、) 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、# include class A int * a; public: A (

10、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 d= d i= i endl; d = 88; i = 9999; f

11、(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; 输出为:10004、# include class Test private: s

12、tatic 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 Result2 = a.func()endl; Test:sfunc(a); 输出为:Result1 =40Result2=80Result3=255、#include template class Tclassprivate:T x,y; public: Tclass(T

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号