C++期末复习(程序分析)_第1页
C++期末复习(程序分析)_第2页
C++期末复习(程序分析)_第3页
C++期末复习(程序分析)_第4页
C++期末复习(程序分析)_第5页
已阅读5页,还剩16页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 T> 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<int> 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&); 出错, t

3、wo 尚未声明 (应在 class one 前加声明语句 class two ; ) 。 4、指出下面程序段中的错误,并说明出错原因。 include <iostream.h> 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;

4、 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 i

5、nitY); int GetX( ); int GetY( ); ;答:int X,Y=20; 出错,不能采用这种方式初始化。6、下面的程序类 B 的定义中有一处错误,请用下横线标出错误所在行并说明错误原因。 # include<iostream.h> # include<string.h> 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;

6、 ; void B:PrintName( )const cout<< " name: " <<name<<endl; /错误,name为私有成员 void main( )B b1( " wang li " ); b1.PrintName( ); 7、用下横线标出下面程序 main 函数中的错误所在行,并说明错误原因。 # include<iostream.h> class Locationprivate: int X,Y; public: void init(int initX,int initY); in

7、t 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; cout<<x+y<< " " <<A1.sumXY( )<<endl; /错误 8、下面的程序有一处错误,请用下横线标出错误所在行并改正错误。# include<iostream.h&g

8、t; class Test public: static int x; ; int x=20; / 对类的静态成员初始化错误 void main ( ) cout<<Test:x; 9、指出下面的程序中的两处错误,并改正class Exampublic:Exam(int y=10) data=y; int getdata() const return +data; /错误常成员函数不能修改对象/修改方法:删除关键字conststatic int getcount()cout<<data;/ 错误静态成员函数不能直接访问非静态成员/修改方法:删除cout这一行return

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

10、 <<endl; pName=new charstrlen(pN)+1; if(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);/对同一个空间的两次释放,修改方法:增加一个复

11、制构造函数Person:Person(Person& p) pName=new charstrlen(p.pName)+1; strcpy(pName,p.pName);五、写出下面程序的执行结果 1、# include <iostream.h> class A int * a; public: A (int x) a = new int(x); cout<<"*a = "<<*a<<endl; delete a; ; void main() A x(3), *p; p = new A (5); delete p; 输

12、出为:*a=3*a=52、# include <iostream.h> template <class T, class Q> 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 <<

13、 endl; d = 88; i = 9999; f(i,d); cout << "d=" << d << " i= " << i << endl; 输出为:d=88 i=88d=9999,i=99993、# include <iostream.h> class base public: virtual int func () return 0; ; class derived: public base public: int func() return 100; ; void ma

14、in() derived d; base & b = d; cout << b.func() << endl; cout << b.base:func() << endl; 输出为:10004、# include <iostream.h> 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 +=

15、val; return val; void Test:sfunc(Test &r) r.a = 25; cout << "Result3 = " << r.a<<endl; void main() cout << "Result1 = " << Test:func() << endl; Test a; cout << "Result2 = " << a.func()<<endl; Test:sfunc(a); 输出为:R

16、esult1 =40Result2=80Result3=255、#include<iostream.h> template<class T> 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 <<(y>(T)0? y:-y)<< "i"&

17、lt;<endl; ; void main( ) Tclass <double> a(10.5,-5.8); a.pr( ); Tclass <int> b(10); b.pr( ); 输出为:10.5-4.8i10+0i6、#include<iostream.h> class testpublic:test()cout <<"调用构造函数"<<endl; ;void f(int i)static test x;cout <<i<<endl; void main()f(10);f(20

18、);输出为:调用构造函数10207、include <iostream.h> void main( ) int *a; int *&p=a; int b=10; p=&b; cout<<*a; 输出为:108、 include <iostream.h> template<class T> Tf(T*a,T*b,int n) Ts=(T)0; for(int i=0;i<n;i+) s+=a i *b i ; return s; void main() double c 5 =1.1,2.2,3.3,4.4,5.5,d 5 =

19、10.0,100.0,1000.0; cout<<f(c,d,5)<<endl; 输出为:3531 9、#include <iomanip.h> void main() for(int i=0;i<4;i+) cout<<endl<<setfill(' ')<<setw(4-i)<< '0' <<setfill('#')<<setw(i+i)<<(i>0?'0':' ' ); 输出为:

20、 0 0#0 0#0 0#0 10、运行下面的程序,写出当输入 25 , 60 时的输出结果。 #include <iostream.h> class goods private: 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; stat

21、ic int getTotal() return totalWeight; ; int goods:totalWeight=0; void main() int w; cout<< "The initial weight of goods:"<<goods:getTotal()<<endl; cin>>w; / 输入 25 goods g1(w); cin>>w; / 输入 60 goods g2(w); cout<< "The total weight of goods:" <

22、;<goods:getTotal()<<endl; 输出为: The initial weight of goods:0 The total weight of goods:8511、# include<iostream.h> class A public: virtual void print( )cout<< " This is class A printing. " <<endl; ; class B:public A public: void print( ) ; class C:public B public:

23、 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<iostream.h> class Apublic: A( )a=0; A(int i)a=i; void Print( )

24、cout<<a<< " , " ; int Geta( )return a; private: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( ); cout<<b<< " , " <<aa.Geta( )<<

25、endl; void main( ) B bb2; bb0=B(1,2,5); bb1=B(3,4,7); for(int i=0;i<2;i+) bbi.Print( ); 输出为:1, 5, 23, 7, 413、#include<iostream.h> 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(

26、Test &r) r.a=125; cout<< "Result3=" <<r.a<<endl; void main( ) cout<< "Result1=" <<Test:func( )<<endl; Test a; cout<< "Result2=" <<a.func( )<<endl; a.sfunc(a); 输出为:Result1 =200Result2=201Result3=12514、# include<

27、;iostream.h> 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( ); cout<<A.valueX( )<<

28、" "<<A.valueY( )<<endl; A.valueX(5); cout<<A.valueX( )<<" "<<A.valueY( )<<endl; B.init(6,2); B.valueY(4); cout<<B.valueX( )<<" "<<B.valueY( )<<endl; 输出为:0 05 06 415、#include <iostream.h> class A public: A

29、( ) virtual void func( )cout<<"Destructor A" <<endl; A( ) func(); ; class B:public A public: B( ) void func()cout<<"Destructor B" <<endl; B( ) func(); ; void main( ) B b; A&a=b; 输出为:Destructor B Destructor A16、 include <iostream.h> class My Class

30、 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); cout<<my1.number<<end1; my1.set(number); cout<<my1.number<<endl; my1.set(:number); cout<<my1.number; 输出为:5 10 3 17、#include<io

31、stream.h> class AAA int A,B; public: AAA(int i,int j) A=i,B=j; cout<<"Cn" AAA()cout<<"Dn" void print( ) cout<<A<<","<<B<<"n" ;void main() AAA *a1,*a2; a1=new AAA(1,2); a2=new AAA(5,6); a1->print(); a2->print(); dele

32、te a1; delete a2; 输出为:CC1,25,6 DD 18、#include <iostream.h> class b1 public: b1(int x) cout<<x<<"->An" b1() cout<<"Bn" ; class b2 public: b2(int x) cout<<x<<"->Cn" b2() cout<<"Dn" ; class AAA:public b2,public b1 p

33、ublic: AAA(int x,int y):b1(y),b2(x) cout<<"En" AAA()cout<<"Fn" ;void main() AAA obj(5,7); 输出为:5->C7->AEFBD19、#include<iostream.h> class FATHER public: virtual void answer( ) cout<<"I am fathern" ; class DAUGHTER:public FATHER public: virtua

34、l void answer( ) cout<<"I am daughtern"class SON:public FATHERpublic: void answer()cout<<"I am sonn" ;void main() FATHER father; DAUGHTER daughter; SON son; FATHER *who; who=&father; who->answer(); who=&daughter; who->answer(); who=&son; who->answ

35、er(); 输出为:I am fatherI am daughterI am son20、#include <iostream.h>class Basepublic: Base()cout<<"Generate Base"<<endl; Base()cout<<"Destroy Base"<<endl; ; class child:public Base public:child():Base()cout<<"Generate child"<<endl

36、; child()cout<<"Destroy child"<<endl; ; void main()child c2;输出为:Generate BaseGenerate childDestroy childDestroy Base21、#include <iostream.h>#include <string.h>class StudentIDpublic: StudentID(int id=0) value=id; cout <<"Assigning student id " <<

37、;value <<endl; StudentID() cout <<"Destructing id " <<value <<endl; protected: int value;class Studentpublic: Student(char* pName="noName",int ssID=0) cout <<"Constructing student " <<pName <<endl; strncpy(name,pName,sizeof(name

38、); 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 id9818Destructing id 022、#include <iostream.h>class MyClasspublic: M

39、yClass(); 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 <<"Dis

40、play a number: " <<number <<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.D

41、estructing.23、#include <iostream.h>#include <string.h>class Student protected: char name40;public: Student(char* pName="no name") cout <<"Constructing new student " <<pName <<endl; strncpy(name,pName,sizeof(name); namesizeof(name)-1='0' Stude

42、nt(Student& s) cout <<"Constructing copy of " << <<endl; strcpy(name,"copy of "); strcat(name,); Student() cout <<"Destructing " <<name <<endl; ;class Tutorprotected: 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"); Tuto

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论