四、完成程序题(本大题共5小题,每小题4分,共20分) 根据题目要求,完成程序填空。 26.请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为: 2,1 4,3 #include<iostream.h> class A { int a; public: A(int i=0){a=i;} int Geta( ){return a;} }; class B { A a; int b; public: B(int i=0,int j=0): ① {} void display( ){cout<<a.Geta()<<′,′<<b<<endl;} }; void main( ) { B b[2]={B(1,2),B(3,4)}; for(int i=0;i<2;i++) ② ; } 27.下面程序中A是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为: B1 called B2 called #include<iostream.h> class A { public: ① ; }; class B1:public A { public: void display( ){cout<<”B1 called”<<endl;} }; class B2:public A { public: void display( ){cout<<”B2 called”<<endl;} }; void show(② ) { p->display( ); } void main( ) { B1 b1; B2 b2; A* p[2]={&b1,&b2}; for(int i=0;i<2;i++) show(p[i]); } 28.请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为: Name:王小明 Grade:90 #include<iostream.h> #include<string.h> class Person { char name[20]; public: Person(char* s){strcpy(name,s);} void display( ){cout<<”Name:”<<name<<endl;} }; class Student:public Person { int grade; public: Student(char* s, int g): ① {grade=g;} void display( ) { ② ; cout<<”Grade:”<<grade<<endl; } }; void main( ) { Student s(“王小明”,90); s.display( ); } 29.请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为5。 #include<iostream.h> class Integer { int x; public: Integer(int a=0){x=a;} void display( ){cout<<x<<endl;} ① ; }; Integer Max(Integer a,Integer b) { if(② ) return a; return b; } void main( ) { Integer a(3),b(5),c; c=Max(a,b); c.display( ); } 30.请在下面的横线处填上适当内容,以使类的定义完整。 class Array { int* ptr; int size; public: Array( ){size=0; ptr=0;} Array(int n){size=n;ptr=new int[size];} Array(① ) //复制初始化构造函数 { size=a.size; ptr=new int[size]; for(int i=0;i<size;i++) ② ; //将源对象的动态数组内容复制到目标对象 } };五、程序分析题(本大题共6小题,每小题5分,共30分) 阅读下面的程序,写出程序运行的结果。 31.#include<iostream.h> class Test { private: int num; public: Test(int n=0){num=n;num++;} ~Test( ){cout<<”Destructor is active,number=”<<num<<endl;} }; void main( ) { Test x[2]; cout<<”Exiting main”<<endl; } 32.#include <iostream.h> class A { public: virtual void fun (int data){cout<<”class A:”<<data<<endl;} void fun(char *str){ cout<<”class A:”<<str<<endl; } }; class B: public A { public: void fun( ) {cout<<”class B”<<endl;} void fun(int data) { cout<<”class B:”<<data<<endl; } void fun(char *str){ cout<<”class B:”<<str<<endl;} }; void main( ) { A *pA; B b; pA=&b; pA->fun(1); pA->fun(“Hello”); } 33.#include <iostream.h> void main( ) { cout.fill('*'); cout.width(10); cout<<"123.45"<<endl; cout.width(8); cout<<"123.45"<<endl; cout.width(4); cout<<"123.45"<<endl; } 34.#include<iostream.h> class Num { int X,Y; public: Num(int x,int y=0){X=x;Y=y;} void value(int x,int y=0){X=x;Y=y;} void value( ){ cout<<X; if(Y!=0) cout<<(Y>0?’+’:’-’)<<(Y>0?Y:-Y)<<’i’; cout<<endl; } }; void main( ) { Num n(1); n.value( ); n.value(2,3); n.value( ); Num m(3,-4); m.value( ); } 35.#include<iostream.h> class Sample { private: int i; static int count; public: Sample( ); void display( ); }; Sample::Sample( ) { i=0; count++; } void Sample::display( ) { cout<<"i="<<i++<<",count="<<count<<endl; } int Sample::count=0; void main( ) { Sample a,b; a.display( ); b.display( ); } 36.#include<iostream.h> class A { int a; public: A(int aa=0){a=aa;cout<<"a="<<a<<endl;} }; class B { int b; public: B(int bb=0){b=bb;cout<<"b="<<b<<endl;} }; class C:public B { A a; public: C( ){cout<<”C default constructor”<<endl;} C(int i,int j):a(i),B(j){cout<<”C constructor”<<endl;} }; void main( ) { C c来源:www.91exam.org1,c2(5,6); } |