高级程序设计C++(II)考试题型 一、选择填空题,20题,20分。
二、写出下列程序运行结果,6题,30分。
三、根据题目要求,将程序的空格处补充完整,3题,24分。 四、按要求编写程序,2题,26分。
题目示范
一、 选择填空题
1.下列函数中,不能有参数的是( C )。 (A) 成员函数 (B) 友元函数 (C) 析构函数 (D) 构造函数
2.在下面类的说明中,正确的语句是( A )。 class Csample {
const int a=10; …………………………………………………. ..(A) Csample(Csample sample);……………………..…………….. (B) Csample(int val):a(val ){} …………………………………..… ..(C) ~Csample(int x);……………………………………………….… .(D) }
3. 若Example是已定义的类,在程序结束之前,共创建了( B )对象。 #include “example.h” main() {
Example example(0), *pExample, Aexample[3]; example.print();
pExample=new example; }
(A) 4 (B) 5 (C) 6 (D) 7
4. 下述关于类的静态成员特性描述中,错误的是( D )。 (A) 静态成员函数没有this指针
(B) 静态数据成员在类中声明,在类外初始化
(C) 在类外引用静态数据成员时,要在静态数据成员名前加<类名>和作用域运算符 (D) 类的每个对象都有自己的静态数据成员副本
5. 下列为类Fraction重载为非成员函数的运算符重载函数原型中,错误的是( C )。
(A) Fraction operator +(Fraction f1, Fraction f2) (B) Fraction operator –(Fraction f1) (C) Fraction &operator=(Fraction &f1)
(D) Fraction operator * (Fraction f1, Fraction f2)
6. 下列为类Example定义重载的提取运算符(“>>”)函数头中,正确的是( A )。 (A) istream &operator>>(istream &input, const Example)
(B) istream& Example::operator>>(istream &intput, const Example) (C)ostream &operator>>(ostream &input, const Example)
(D)ostream& Example::operator>>(ostream &input, const Example)
7. 在下列运算符中,不能被重载的是( C )。 (A) [ ] (B) () (C) :? (D) new
8. 在建立派生类对象时,可能要调用的三种构造函数分别是a(基类的构造函数),b(成员对象的构造函数),c(派生类的构造函数). 这三种构造函数的调用顺序是( A )。 (A) abc (B) acb (C) cab (D) cba
9. 下列程序中划线处应填入的语句是( C )。 #include
public:
void fun() {cout<<”Base of fun”< class Derived:public Base { ___________; //调用基类的成员函数fun cout<<”Derived of fun”< (A)Fun() (B) Base.fun() (C) Base::fun() (D) Base->fun() 10. 下面程序中错误的语句是( C )。 class Base { public: virtual void f()=0; virtual ~a(){} }; class Derived: public Base { protected: int a,b; public: void f() { cin>>a>>b; cout< void main() { Base *pBase=new Derived; pBase->f();…………………………….(A) Derived derive;………………………..(B) derive.Base::f();……………………….(C) Delete pBase;………………………….(D) } 二、 读程序,写运行结果。(每小题4分,共20分) 1. #include int n; static int sum; public: sample(int x){ n=x;} void add(){sum+=n;} void disp() { cout< int sample::sum=0; int main() { sample a(2),b(3),c(5); a.add(); a.disp(); b.add(); b.disp(); c.add(); c.disp(); } 2. #include public: virtual void f() {cout<<\< {cout<<\< class B:public A { public: void f() {cout<<\< {cout<<\< int main() { A a;B b; a=b; a.f(); a.g(); A*pa=&b; pa->f();pa->g(); } 三、 读程序填空。 1. 以下程序的功能是定义字符串类,实现字符串的连接与赋值。程序的运行结果如下图所 示。请将程序补充完整。 #include _________(1)__________ //构造函数STRING(char *pn) { size=strlen(pn); string=new char[size+1]; if (string!=0) strcpy_s(string,size+1,pn); } void show() //显示输出 { cout< STRING operator+( STRING obj ); // 重载加法运算 STRING & operator=(const STRING &); //重载赋值运算 private: char *string; int size; }; STRING STRING::operator+( STRING obj ) { STRING temp(\); temp.size=size+obj.size; __________(2)_____________;// temp.string=new char[temp.size+1] strcpy_s(temp.string,size+1,string); strcat_s( temp.string,temp.size+1,obj.string ); return temp; } STRING & STRING::operator =(const STRING &obj) { size=obj.size; delete[]string; string=new char[size+1]; 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库高级程序设计C++(二)考试题型及题例-答案在线全文阅读。
相关推荐: