C++期末考试程序分析题30道

1. 下面程序的运行结果是____

   #include<iostream>

   using namespace std;

   class Base

{public:

   Base(){ cout << "0"; }

};

Class Base1: virtual Base

{public:

   Base1(){ cout << "1"; }

};

Class Base2: virtual Base

{public:

   Base2(){ cout << "2"; }

};

class Derived: public Base1, public Base2

{public:

   Derived(){ cout << "3"; }

};

void main()

{  Derived obj;

}

A. 0123      B. 3120      C. 0312       D. 3012

答案:A

 

2. 写出程序运行结果

#include<iostream>

   using namespace std;

   int i = 0;

   void fun()

   {

       {

           static int i = 1;

           std::cout << i++ << ',';

       }

       std::cout << i << ',';

}

int main()

{

    fun();    fun();

    return 0;

}

答案:1,0,2,0,

 

3. 下列程序中画线处应填如的语句是____

   class Base

{public:

   void fun(){ cout << "Base of fun" << endl; }

};

class Derived: public Base

{public:

   void fun()

   {

      ______ //调用基类的成员函数fun()

      cout << "Derived of fun" << endl;

}

};

A. fun();                    B. Base.fun();   

C. Base::fun();          D. Base->fun() ;

答案:C

 

4. 写出程序运行结果

#include<iostream>

   using namespace std;

   class Base

{public:

   int x;

   void Print(){ cout << "Base::x is " << x << endl; }

};

class Derived1: public Base

{public:

   void Print()

{  cout << "Derived1::x is " << x << endl;

}

};

class Derived2: private Base

{public:

   Derived2(){ Base::x = 4; }

   int x;

   void Print()

{  cout << "Derived2's Base::x is " << Base::x << endl;

   cout << "Derived2::x is " << x << endl;

}

};

void main()

{  Base objB;

   Derived1 objD1;

   Derived2 objD2;

objD2.x = 1 + (objD1.x = 1 + (objB.x = 1));

objB.Print();

objD1.Print();

objD2.Print();

}

答案:Base::x is 1

           Derived1::x is 2

           Derived2’s Base::x is 4

           Derived2::x is 3

 

5. 写出程序运行结果

   #include<iostream>

   using namespace std;

   class A

{public:

    A(){ cout << "constructor of A" << endl; }

    ~A(){ cout << "destructor of A" << endl; }

};

class B

{public:

    B(){ cout << "constructor of B" << endl; }

    ~B(){ cout << "destructor of B" << endl; }

};

int main()

{   B b;

    static A a;

   }

答案:constructor of B

           constructor of A

           constructor of B

           constructor of A

 

6. 写出程序运行结果

   #include<iostream>

   using namespace std;

   class Coord

{public:

   Coord(int i = 0, int j = 0){ x = i; y = j; }

   void Print(){ cout << "x=" << x << ", y=" << y << endl; }

   friend Coord operator++(Coord op);

private:

   int x, y;

};

Coord operator ++(Coord op)

{  ++op.x;

   ++op.y;

   return op;

}

void main()

{  Coord obj(1, 2);

   obj.Print();

   ++obj;

   obj.Print();

}

答案:x = 1, y = 2

           x = 1, y = 2

 

7. 写出程序运行结果

#include <iostream>

using namespace std;

class Expt{};

class Expt1: public Expt{

public: Expt1() { cout<<"Expt1 的构造函数被调用。"<<endl; }

Expt1(Expt1 & e) {cout<<"Expt1 的拷贝构造函数被调用。"<<endl; }

~Expt1() { cout<<"Expt1 的析构函数被调用。"<<endl; }

};

class Expt2: public Expt1 {

public: Expt2() { cout<<"Expt2 的构造函数被调用。"<<endl; }

Expt2(Expt2 & e) { cout<<"Expt2 的拷贝构造函数被调用。"<<endl; }

~Expt2()  { cout<<"Expt2 的析构函数被调用。"<<endl; }

};

void MyFunc() {    Expt1 e; throw e; }

void main() {

    try  {

        cout << "在try块中,调用MyFunc()。" << endl;

        MyFunc();

        cout << "在try块中,MyFunc()执行结束。" << endl;

    }

    catch( Expt E )    {  cout << "捕获到Expt类型异常。"<<endl; }

catch( Expt2 E ) {   cout << "捕获到Expt2类型异常。"<<endl;  }

    cout << "回到main函数。" << endl;

}

答案:在try块中,调用MyFunc()。

           Expt1 的构造函数被调用。

           Expt1 的拷贝构造函数被调用。

           Expt1 的析构函数被调用。

           捕获到Expt类型异常。

           Expt1 的析构函数被调用。

           回到main函数。

 

8. 下面的程序有什么错误?

   class MyClass

{

public:

    void MyClass(int a){ x = a; }

    int f(int a, int b){ x = a; y = b; }

    int f(int a, int b, int c = 0){ x = a; y = b; z = c; }

    static void g(){ x = 10; };

private:

    int x, y, z;

};

答案:构造函数不能有返回值和返回值类型;静态函数g不能直接访问非静态数据成员x。

 

9. 写出程序运行结果

#include<iostream>

using namespace std;

class Shape{

public:

Shape() { cout<<"Shape  的构造函数被调用。"<<endl; }

void Display() { cout<<"Shape 的显示函数被调用。"<<endl; }

virtual ~Shape() { cout<<"Shape 的析构函数被调用。"<<endl;  }

};

class Rectangle: public Shape {

public: Rectangle () {

Side=Hight=0;     

   cout<<"Rectangle 的默认构造函数被调用。"<<endl;     

   }

            Rectangle(int xx,int yy)   {

   Side=xx;   Hight=yy;    

   cout<< "Rectangle 的构造函数被调用。"<<endl;     

}

            void Display() {

cout<<" Rectangle 的边长和高为:"<<Side<<","<<Hight<<endl;

}

    ~Rectangle()   {    cout<<"Rectangle 的析构函数被调用。"<<endl;   }

         int GetSide() {return Side;}

         int GetHight() {return Hight;}

 private:

       int  Side, Hight;

};

void main()

{

     Shape *ptr1 = new Rectangle[2];

     (*(ptr1+1)).Display();

 delete [] ptr1;

}

答案:Shape的构造函数被调用。

           Rectangle的默认构造函数被调用。

           Shape的构造函数被调用。

           Rectangle的默认构造函数被调用。

           Shape的显示函数被调用。

           Rectangle的析构函数被调用。

           Shape的析构函数被调用。

           Rectangle的析构函数被调用。

           Shape的析构函数被调用。

 

10. 写出程序运行结果

   #include<iostream>

   using namespace std;

   template<class T>

   class Sample

{private:

   T n;

public:

   Sample(T i){ n = i; }

   void operator++();

void disp(){ cout << "n = " << n << endl; }

};

template<class T>

void Sample<T>::operator++()

{  n += 1;

}

void main()

{  Sample<char> s('a');

   s++;

   s.disp();

}

答案:n = b

 

11. 写出程序运行结果

#include <iostream>

using namespace std;

char* increase(char* ch) {return ++ch;}

char& increase(char& ch) {return ++ch;}

int main(){

char buf[16] = "IloveC++!";

char* p = buf;

for (int i = 0; i < 4; i++) {

p = increase(p);

increase(*p);

}

cout << buf << endl;

return 0;

}

答案:ImpwfC++!

 

12. 写出程序运行结果

 #include<iostream>

   using namespace std;

   class Sample

{

public:

    int x, y;

    Sample(int a = 0, int b = 0){ x = a; y = b; }

    friend void square_x(Sample &s){ s.x = s.x * s.x; }

    void disp(){ cout << "x = " << x << ", y = " << y << endl; }

};

void main()

{

    Sample s(2, 3);

    square_x(s);

    s.disp();

}

答案:x = 4, y = 3

 

13. 写出程序运行结果

   #include<iostream>

   using namespace std;

   class A

{

    int num;

public:

    A(int i){ num = i; }

    A(A &a){ num = a.num++; }

    void print(){ cout << num; }

};

void main()

{

    A a(1), b(a);

    a.print();

    b.print();

}

答案:21

 

14. 写出程序运行结果

   #include<iostream>

   using namespace std;

   class Base

   {public:

      int n;

      Base(int x){ n = x; }

      virtual void set(int m){ n = m; cout << n << ' '; }

   };

   class DerivedA: public Base

{public:

   DerivedA(int x): Base(x){  }

   void set(int m){ n += m; cout << n << ' '; }

};

class DerivedB: public Base

{public:

   DerivedB(int x): Base(x){  }

   void set(int m){ n += m; cout << n << ' '; }

};

int main()

{  DerivedA da(1);

   DerivedB db(3);

   Base * pBase;

   pBase = &da;

   pBase->set(1);

   pBase = &db;

   pBase->set(2);

}

答案:2 5

 

15. 写出程序运行结果

#include <iostream>

using namespace std ;

class Base

{public:

virtual void fun(int a) { cout << "Base: " << a << endl; }

void fun(char *p) { cout << "Base: " << p << endl; }

void fun() { cout << "Base: no parameter!" << endl; }

};

class Derived: public Base

{public:

void fun(int a) { cout << "Derived:" << a << endl; }

void fun(char *p) { cout << "Derived:" << p << endl; }

};

void main()

{ Derived* pd = new Derived;

Base *pb = pd;

pb->fun(); pb->fun(0); pb->fun("c++");

pd->fun(1); pd->fun("object");

}

答案:Base: no parameter!

           Derived:0

           Base: c++

           Derived:1

           Derived:object

 

16. 写出程序运行结果

   #include<iostream>

   using namespace std;

   class Sample

{

public:

    int x, y;

    Sample(int a = 0, int b = 0){ x = a; y = b; }

    ~Sample(){ cout << "x = " << x << ", y = " << y << endl; }

};

void main()

{

    Sample s;

    s.~Sample();

}

答案:x = 0, y = 0

           x = 0, y = 0

 

17. 写出程序运行结果

   #include<iostream>

   Using namespace std;

   class First

{protected:

   int first;

public:

   First(int f = 0){ first = f; }

   void show(){ cout << first << endl; }

};

class Second: public First

{private:

   int second;

public:

   Second(int f, int s): First(f), second(s) {  }

   void show(){ cout << first << " " << second << endl; }

};

int main()

{

   Second s(5, 3);

   s.show();

}

A. 5 3             B. 3           C. 5             D. 3 5

答案:A

 

18. 写出程序运行结果

   #include<iostream>

   using namespace std;

   class Base

   {int n;

   public:

      Base(int a)

      {  cout << "constructing base class" << endl;

         n = a;

         cout << "n = " << n << endl;

      }

      ~Base(){ cout << "destructing base class" << endl; }

   };

   class Derived: public Base

   {  Base base;

      int m;

   public:

      Derived(int a, int b, int c) : Base(a), base(c)

      {  cout << "constructing derived class" << endl;

         m = b;

         cout << "m = " << m << endl;

}

      ~Derived(){ cout << "destructing derived class" << endl; }

};

void main(){ Derived d(1, 2, 3); }

答案:constructing base class

           n = 1

           constructing base class

           n = 3

           constructing derived class

           m = 2

           destructing derived class

           destructing base class

           destructing base class

 

19. 写出程序运行结果

#include <iostream>

using namespace std;

class B1 {

public:   B1(int i) {cout<<"B1 的构造函数被调用:"<<i<<endl;}

~B1() {cout<<"B1 的析构函数被调用。"<<endl;}

};

class B2 {

public: B2(int j) {cout<<"B2 的构造函数被调用:"<<j<<endl;}

~B2() {cout<<"B2 的析构函数被调用。"<<endl;}

};

class C: public B2, public B1 {

public:  

C(int a, int b, int c, int d):B1(a),memberB2(d),memberB1(c),B2(b) {

cout<<"C 的构造函数被调用。"<<endl;

 }

~C()  { cout<<"C 的析构函数被调用。"<<endl;}

private:  

B1 memberB1;  B2 memberB2;

};

void main(){

C obj(1,2,3,4); 

}

答案:B2的构造函数被调用:2

           B1的构造函数被调用:1

           B1的构造函数被调用:3

           B2的构造函数被调用:4

           C的构造函数被调用。

           C的析构函数被调用。

           B2的析构函数被调用。

           B1的析构函数被调用。

           B1的析构函数被调用。

           B2的析构函数被调用。

 

20. 写出程序运行结果

#include<iostream>

using namespace std;

void func(int a)

{

    static int m = 0;

    m += a;

    cout << m << " ";

}

void main()

{

    int k = 6;

    func(k);

    func(k);

    cout << endl;

}

答案:6 12

 

21. 下面程序的输出是____

   #include<iostream>

   using namespace std;

   class Sample

{

public:

    int x, y;

    Sample(int a = 0, int b = 0){ x = a; y = b; }

    ~Sample(){ cout << "x = " << x << ", y = " << y << endl; }

};

void main()

{

    Sample s;

    exit(0);

}

答案:(不输出)

 

22. 写出程序运行结果

#include <iostream>

using namespace std;

class point {

protected:

int x, y;

public:  

point(int xx, int yy) {

x = xx, y = yy;

cout << "construct point: " << x << "," << y << endl;

}

~point() { cout << "destroy point" << endl;}

};

class circle {

protected:

point center;

int   radius;

public:  

circle(int x, int y, int r):center(x, y),radius(r) {

cout << "construct circle: " << radius << endl;

  }

~circle(){ cout << "destroy circle " << endl;}

};

int main() {

circle c(1,2,3);

return 0;

答案:construct point:1,2

           construct circle:3

           destroy circle

           destroy point

 

23. 下面程序的执行结果是:

#include<iostream>

using namespace std;

class Sample

{  int x;

public:

    Sample(int a)

    {  x = a;

        cout << "constructing object: x = " << x << endl;

    }

};

void fun(int n)

{  static Sample obj(n);

}

void main()

{  fun(1); fun(10);

}

答案:constructing object: x = 1

 

24. 下面程序的执行结果是____

   #include<iostream>

   using namespace std;

   class Sample

{

public:

    int x, y;

    void disp(){ cout << "x = " << x << ", y = " << y << endl; }

};

void main()

{

    int Sample::*pc;

    Sample s;

    pc = &Sample::x;

    s.*pc = 10;

    pc = &Sample::y;

    s.*pc = 20;

    s.disp();

}

答案:x = 10, y = 20

 

25.  有以下程序:

   #include<iostream>

   using namespace std;

   class MyClass

{

   public:

       MyClass(int n){ number = n; }

       MyClass(MyClass & other){ number = other.number; }

       ~MyClass(){}

   private:

       int number;

};

   MyClass fun(MyClass p)

   {

       MyClass temp(p);

       return temp;

   }

   int main()

   {

       MyClass obj1(10), obj2(0);

       MyClass obj3(obj1);

       obj2 = fun(obj3);

       return 0;

   }

程序执行时,MyClass的拷贝构造函数被调用了几次?

答案:4次

 

26. 有以下定义:

   class Point

{

public:

    Point(int x = 0, int y = 0){ _x = x; _y = y; }

    void Move(int xOff, int yOff)

    { _x += xOff; _y += yOff; }

    void Print() const

    { cout << '(' << _x << ',' << _y << ')' << endl; }

private:

    int _x, _y;

};

下列语句中会发生编译错误的是____

A. Point pt;    pt.Print();

B. const Point pt;    pt.Print();

C. Point pt;    pt.Move(1, 2);

D. const Point pt;    pt.Move(1, 2);

答案:D

 

27. 写出程序运行结果

  #include<iostream>

using namespace std;

char *x[] = {"first", "second", "third"};

void fun(char *z[])

{

    cout << *++z << endl;

}

int main()

{

    char **y;

    y = x;

    fun(y);

    return 0;

}

答案:second

 

28. 写出程序运行结果

#include <iostream>

using namespace std;

class counter {

protected:

static int count;

int id;

public:

counter() {

id = ++ count;

cout << "id = " << id << ", count = " << count << endl;

}

};

int counter::count = 0;

static counter c[3];

int main() {

cout << "initialize program" << endl;

return 0;

}

答案:id=1,count=1

           id=2,count=2

           id=3,count=3

           initialize program

 

29. 写出程序运行结果

#include <iostream>

using namespace std;

class Expt{};

class Expt1: public Expt{

public: Expt1() { cout<<"Expt1 的构造函数被调用。"<<endl; }

Expt1(Expt1 & e) {cout<<"Expt1 的拷贝构造函数被调用。"<<endl; }

~Expt1() { cout<<"Expt1 的析构函数被调用。"<<endl; }

};

class Expt2: public Expt1 {

public: Expt2() { cout<<"Expt2 的构造函数被调用。"<<endl; }

Expt2(Expt2 & e) { cout<<"Expt2 的拷贝构造函数被调用。"<<endl; }

~Expt2()  { cout<<"Expt2 的析构函数被调用。"<<endl; }

};

void MyFunc() {    Expt1 e; throw e; }

void main() {

    try  {

        cout << "在try块中,调用MyFunc()。" << endl;

        MyFunc();

        cout << "在try块中,MyFunc()执行结束。" << endl;

    }

    catch( Expt E )    {  cout << "捕获到Expt类型异常。"<<endl; }

catch( Expt2 E ) {   cout << "捕获到Expt2类型异常。"<<endl;  }

    cout << "回到main函数。" << endl;

}

答案:在try块中,调用MyFunc()。

           Expt1 的构造函数被调用。

           Expt1 的拷贝构造函数被调用。

           Expt1 的析构函数被调用。

           捕获到Expt类型异常。

           Expt1 的析构函数被调用。

           回到main函数。

 

30. 有如下动态数组模板类Array的声明,请在空格处填入最合适的内容。

_(1)_ <class T>

class Array

{protected:

    int size;               //动态数组的长度

     (2)  elements;        //动态数组的首地址

public:

    Array(int i);

     (3)  ~Array();        //析构函数

    Array( (4)  a);        //拷贝构造函数

     (5)  operator[](int index);   //重载下标运算符

     (6)  ostream & operator <<(ostream &o, Array & a);

};

(1) 空格(1)处应该填写 【      】

A. virtual    B. template     C. void     D. class

(2) 空格(2)处应该填写 【      】

A. T *         B. T             C. int *    D. int

(3) 空格(3)处应该填写 【      】

A. void     B. int        C. virtual   D. template

(4) 空格(4)处应该填写 【      】

A. Array    B. Array *   C. Array &   D. const Array &

(5) 空格(5)处应该填写 【      】

A. T         B. T *        C. T &       D. void

(6) 空格(6)处应该填写 【      】

A. friend   B. const     C. virtual   D. void

答案:B A C D C A

猜你喜欢

转载自blog.csdn.net/m0_63947712/article/details/128336468