《最新C++期末复习(程序分析).doc》由会员分享,可在线阅读,更多相关《最新C++期末复习(程序分析).doc(38页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精品资料C+期末复习(程序分析).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;
2、; void main()Tany (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 前加声明
3、语句 class two ; ) 。 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() 有
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 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 函数中的错误所在行,并说明错误原
6、因。 # include class 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这一
8、行return +count ; 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(p
9、Name,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、# include class A int
10、 * 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 d= d i= i endl; d
11、= 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; 输出为:10004、# include clas
12、s 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 Result2 = a.func()endl; Test
13、:sfunc(a); 输出为:Result1 =40Result2=80Result3=255、#include template class Tclassprivate: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.pr( ); 输出为:10.5-4.8i10+0i6
14、、#include class testpublic: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+) s+=a i *b i ; return s; void main()
15、 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 private: static int totalWeight; int weight;
16、 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 initial weight of goods:goods:getTotal()w;
17、/ 输入 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:public A public: void print( ) ; class
18、 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 Apublic: A( )a=0; A(int i)a=i; void Print( )couta , ; int Geta( )return a; pr
19、ivate:int a; ; class B:public Apublic: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(int i=0;i2;i+) bbi.Print( ); 输出为:1, 5, 23, 7, 413、#
20、include class Testprivate: 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( )endl; Test a; cout Result2= a.func( )endl; a.sfunc(a);
21、输出为:Result1 =200Result2=201Result3=12514、# include class Locationint 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 initY) X=initX; Y=initY; void main( ) Location A,B; A.init(
22、 ); 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 05 06 415、#include class A public: A( ) virtual void func( )coutDestructor A endl; A( ) func(); ; class B:public A public: B( ) void func()coutDestructo
23、r 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); coutmy1.numberend1; my1.set(number); coutmy1.numberend
24、l; 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
25、 x) coutxCn; b2() coutDn; ; class AAA:public b2,public b1 public: AAA(int x,int y):b1(y),b2(x) coutEn; AAA()coutC7-AEFBD19、#include class FATHER public: virtual void answer( ) coutI am fathern; ; class DAUGHTER:public FATHER public: virtual void answer( ) coutI am daughtern;class SON:public FATHERpu
26、blic: void answer()coutanswer(); who=&daughter; who-answer(); who=&son; who-answer(); 输出为:I am fatherI am daughterI am son20、#include class Basepublic: Base()coutGenerate Baseendl; Base()coutDestroy Baseendl; ; class child:public Base public:child():Base()coutGenerate childendl; child()coutDestroy c
27、hildendl; ; void main()child c2;输出为:Generate BaseGenerate childDestroy childDestroy Base21、#include #include class StudentIDpublic: StudentID(int id=0) value=id; cout Assigning student id value endl; StudentID() cout Destructing id value endl; protected: int value;class Studentpublic: Student(char*
28、pName=noName,int ssID=0) cout Constructing student pName endl; strncpy(name,pName,sizeof(name); namesizeof(name)-1=0; StudentID id(ssID); protected: char name20; StudentID id;void main() Student s(Randy,9818);输出为:Assigning student id 0Constructing student RandyAssigning student id 9818Destructing id
29、9818Destructing id 022、#include class MyClasspublic: MyClass(); MyClass(int); MyClass(); void Display();protected: int number;MyClass:MyClass() cout Constructing normally.n; MyClass:MyClass(int m): number(m) cout Constructing with a number: number endl;void MyClass:Display() cout Display a number: n
30、umber endl; MyClass:MyClass() cout Destructing.n; void main() MyClass obj1; MyClass obj2(20); obj1.Display(); obj2.Display();输出为:Constructing normally.Constructing with a number: 20Display a number: 1/此处为不确定的数Display a number: 20Destructing.Destructing.23、#include #include class Student protected: c
31、har name40;public: Student(char* pName=no name) cout Constructing new student pName endl; strncpy(name,pName,sizeof(name); namesizeof(name)-1=0; Student(Student& s) cout Constructing copy of s.name endl; strcpy(name,copy of ); strcat(name,s.name); Student() cout Destructing name endl; ;class Tutorpr
32、otected: Student student;public: Tutor(Student& s):student(s) cout Constructing tutorn; ;void fn(Tutor tutor) cout In function fn()n;void main() Student randy(Randy); Tutor tutor(randy); cout Calling fn()n; fn(tutor); cout Returned from fn()n; 输出为:Constructing new student RandyConstructing copy of R
33、andyConstructing tutorCalling fn()Constructing copy of copy of RandyIn function fn()Destructing copy of copy of RandyReturned from fn()Destructing copy of RandyDestructing Randy24、#include class Samppublic: void Setij(int a, int b)i=a,j=b; Samp() cout Destroying. i endl; int GetMulti() return i*j; p
34、rotected: int i, j;void main() Samp* p = new Samp10; for(int j=0; j10; j+) pj.Setij(j,j); for(int k=0; k10; k+) cout Multi k is: pk.GetMulti() endl; deletep;输出为:Multi0 is:0Multi1 is:1Multi2 is:4Multi3 is:9Multi4 is:16Multi5 is:25Multi6 is:36Multi7 is:49Multi8 is:64Multi9 is:81Destroying.9Destroying.
35、8Destroying.7Destroying.6Destroying.5Destroying.4Destroying.3Destroying.2Destroying.1Destroying.025、#includeclass Basepublic: virtual void fn() cout In Base classn; ;class SubClass :public Basepublic: virtual void fn() cout In SubClassn; ;void test(Base& b) b.fn();void main() Base bc; SubClass sc; cout Calling test(bc)n; test(bc); cout Calling test(sc)n; test(sc);输出为:Calling test(bc)In Base classCalling test(bc)In SubClass