面向对象程序设计试题 课程代码:02328
一、单选
1、程序设计中,首先在问题域中识别出若干个( )
A.函数 B.类 C.文件 D.过程
2、定义类模板用关键字( )
A.const B.new C.delete D.template
3、运算结果类型相同的( )
A. 9.0/2.0 9.0/2
B. 9/2.0 9/2
C. 9.0/2 9/2
D. 9/2 9.0/2.0
4、已知f1 f2同一类两个成员函数,但f1不能调用f2,说明( )
a.f1 f2都是静态函数
b.f1是静态,f2不是
c.f1不是,f2是静态
d.f1 f2都不是静态函数
5、调用一成员函数时,使用动态联编的情况是( )
A.通过对象调用一虚函数 B.通过指针或引用调用一虚函数
C.通过对象调用静态函数 D.通过指针或引用调用一静态函数
6、假定一个类构造函数为:“A(int aa=1,int bb=0){a=aa;b=bb;}则执行"A x(4)"后,x.a和x.b值分别是:( )
A.1,0 B.1,4 C.4,0 D.4,1
7、在派生类中能直接访问基类的( )
A.公有成员,私有成员
B.保护成员,私有成员
C.不可访问成员,私有成员
D.公有成员,保护成员
8、不具访问权限属性的是:( )
A.非类成员 B.类成员 C.数据成员 D.函数成员
9、类定义中private,protected,public 出现次数为( )
A.任意多次 B.至多一次 C.public 至少一次 D.至少一次
10、C++鼓励程序员将( )
A.数据操作分别封装
B.不同类型数据封装
C.数据操作封装在一起
D.不同作用操作封装在一起
答案:B D A B B C D A A C
二、填空
1、C++中,最好用( )代替malloc
2、函数模板中template之后尖括号的类型参数冠以保留字( )
3、在IOS类中定义的用于格式控制的枚举变量中十、八、十六进制是dec,oct,( )
4、如果重载了运算符+,则相应运算函数名是( )
5、由static修饰的数据成员为该类的所有对象( )
6、为了实现多态性,派生类需要重新定义基类中的( )
7、编译时多态性通过()函数实现。
8、派生类中实现基类成员初始化,需由派生类的构造函数调用()来完成。
9、C++中访问指令所指对象的成员使用运算符( )
10、重载函数在参数类型或参数个数上不同但( )相同。
答案:1new 2class 3hex 4operator + 5共享 6同名函数
7重载(函数重载和运算符重载) 8参数表 9 . 10函数名
三、改错
1、类定义有错,正确结果为5+8i
#include #include class complex { double real; double imag; public: complex(double r=0.0,double i=0.0):real(r),imag(i){}; void show() { cout<<<(imag>=0?'+':'-')< friend complex&operator +=(complex c1,complex c2){ 这里应改为: friend complex&operator +=(complex& c1,complex& c2){ c1.real+=c2.real; c1.imag+=c2.imag; return c1; } }; void main() { complex c(3,5); c+=complex(2,3); c.show(); } 2、改一处错 #include class shape{ public: int area(){return 0;} }; class rectangle:public shape{ public: int a,b; void setlength(int x,int y){a=x;b=y;} int area(){return a*b;}}; void main(){ rectangle r; r.setlength(3,5); shape *s=r; 应改为: shape *s=&r; cout < #include class CU{ enum{int,float}type; union value{ int ivalue; floadt fvalue;} 共用体在这里没有定义变量就使用了。 public: CU(int x):type(int),ivalue(x){} CU(float y):type(float),fvalue(y){} void print(){ if (type == int) cout< else cout< CU fCU((floadt)5.6); CU iCU(8) fCU.print(); cout< 4、改一处错 class A{ int a,b; public:A(int aa=0,int bb) {a=aa;b=bb; }} 默认参数值应该从右向左定义。 5、找一处错 class Location{ int x,y; protected: int SetZero(int zeroX,int XeroY) private: int length,height; public: void Locattion (int initX,int initY);此处不应该有返回类型 int getx(); int gety(); } 四、程序填空 1、使输出结果为: 5 4 3 2 1 0 5.5 4.4 3.3 2.2 1.1 #include template void f( T t; for (int i=0;i t=a[i];a[i]=a[n-1-i];a[n-1-i]=t; } void main{ int a[5]={1,2,3,4,5}; doublie d[6]={1.1,2.2,3.3,4.4,5.5} f(a,5); f(d,6); for(int i=0;i<5;i++) cout cout < cout cout < 2、使类定义完整 class line; class box{ private: int color; int upx,upy; int lowx,lowy; public: friend int same_color(line l,box b); void set_color(int c){color =c;} void define_box(int x1,int y1,int x2,int y2) {upx=x1;upy=y1;lowx=x2;lowy=y2;} }; class line{ private: int color; int startx,starty; int endx,endy; public: friend int same_color(line l,box b); void set_color(int C){color=c;} void define_line(int x1,int y1,int x2,int y2) {startx=x1;starty=y1;endx=x2;endy=y2;} }; int same_color(line l,box b){ if (l.color==b.color) return l; return 0; } 3、A为抽象类,输出为: this is class B printing this is class C printing #include class A{ public : virtule void printMe()=0;}; class B:public A{ public: void printMe(){cout<<"this is class B printing"< class C:public B{ void printMe(){cout<<"this is class C printing"< void print(A &a){ a.printMe(); } void main(){ B b; C c; print(b); print(c); } 4、使类完整 class A{ int * a; int n; public: A():a(0),n(0){} A(int nn){ n=nn//用NN初始化N a=new int[n]//用A指向长度为N的动态数组空间 }; 5、使类完整 class base{ protected: int a; public: base(){a=0;} base(int i){a=i} base(base&b){a=b.a}}; class derived:public base{ private: int d; public: derived(){d=0;} derived(int i,int j):base(i){d=j;} derived(derived&b):base(b){d=b.d;} }; 五、程序分析,给出输出结果 1. #include template void f(T *a,int n){ int k; T t; for (int i=0;i k=i; for (int j=i+1;j k=j; t=a[i];a[i]=a[k];a[k]=t;} }; void main(){ double d[5]={12.3,4.5,-23.4,-90.4,0}; char a[5]={'B','F','A','X','E'}; f(a,5); f(d,5); for (int i=0;i<5;i++) cout} -23.4 B 0 E 4.5 F 12.3 X 2. #include void main(){ cout< 123456 1.235e+005 3. #include class goods{ private: static int totalweight; int weight; public: goods(int w){ weight=w; totalweight+=weight; } goods(goods&gd){ weight=gd.weight; totalweight+=weight; } ~goods(){ totalweight-=weight; } static int gettotal(){ return totalweight; } }; int goods::totalweight=0; void main(){ goods g1(50); cout< cout<< 50 150 4. #include class A{ public: A(int i=0){a=i;} void print(){cout<<<',';} private: int a; }; class B:public A{ public: B(){b1=b2=0;} B(int i){b1=i;b2=0;} B(int i,int j,int k):A(i),b1(j),b2(k){} void print(){A::print();cout<<<','<< int b1,b2; }; void main(){ B d1,d2(5),d3(4,5,6); d1.print();d2.print();d3.print(); } 0,0,0 0,5,0 4,5,6 5. #include class A{ public: virtual void pr(){cout<<"1"< class B:public A{ void pr(){cout<<"2"< void p1(A&a) {a.pr();} void p2(A a) {a.pr();} void main(){ B b; p1(b); p2(b); } 2 1 6. #include class shownumtype{ public: void show(int); void show(float); }; void shownumtype::show(int i) {cout<<"this is an int"< {cout<<"this is float "< int a=0;float f=1.0f; shownumtype snt; snt.show(a); snt.show(f); } this is an int this is float下载本文