面向对象程序设计程序题

  • 后续可能会补一下解析

  • pc端善用crtl+f搜索


1

以下程序执行结果是________________。

#include <iostream.h>

class base

{

public:

base()

{ cout << “构造base子对象” << endl;

f(); }

virtual void f()

{ cout << “调用base::f()” << endl;}

};

class derived : public base

{

public:

derived()

{ cout << “构造derived对象” << endl;

f();

}

void f()

{ cout << “调用derived :: f()” << endl;}

};

void main()

{ derived d;

}

正确答案:

构造base子对象

调用base::f()

构造derived对象

调用derived :: f()

2

以下程序执行结果是________________。

#include <iostream.h>

class base

{

public:

base() {

cout << “Constructor base subobject” << endl;

f(); }

virtual void f() {

cout << “Call base::f()” << endl; }

};

class derived : public base

{

public:

derived() {

cout << “Constructor derived object” << endl;

f(); }

void f() {

cout << “Call derived :: f()” << endl; }

};

void main(){

derived d;

}

正确答案:

Constructor base subobject

Call base::f()

Constructor derived object

Call derived :: f()

3

以下程序执行结果是________________。

#include <iostream.h>

class Sample

{

private:

int x;

public:

Sample() {x = 0;}

void disp()

{

cout << “x=” << x << endl;

}

void operator ++() {x += 10;}

};

void main()

{

Sample obj;

obj.disp();

obj ++;

cout << “执行obj++之后” << endl;

obj.disp();

}

正确答案:

x=0

执行obj++之后

x=10

4

以下程序执行结果是

#include <iostream.h>

class base

{

public:

virtual void f1()

{ cout<<"f1 fuc of base"<<endl;}

virtual void f2()

{ cout<<"f2 fuc of base"<<endl;}

virtual void f3()

{ cout<<"f3 fuc of base"<<endl;}

void f4()

{ cout<<"f4 fuc of base"<<endl;}

};

class derive : public base

{

void f1()

{ cout<<"f1 fuc of derive"<<endl;}

void f2(int x)

{ cout<<"f2 fuc of derive"<<endl;}

void f4()

{ cout<<"f4 fuc of derive"<<endl;}

};

void main()

{

base obj1, *p;

derive obj2;

p = &obj1;

p->f1();

p->f2();

p->f3();

p = &obj2;

p->f1();

p->f2();

p->f4();

}

正确答案:

f1 fuc of base

f2 fuc of base

f3 fuc of base

f1 fuc of derive

f2 fuc of base

f4 fuc of base

5

以下程序执行结果是________________。

#include <iostream.h>

class A

{

public:

A(int i, int j) {a = i; b = j;}

void move (int x, int y) {a += x; b += y;}

void show() { cout << “(“ << a << “,” << b << “)” << endl;}

private:

int a, b;

};

class B:private A

{

public:

B(int i, int j, int k, int l):A(i, j)

{ x =k; y = l;}

void show()

{ cout << x << “,” << y << endl;}

void fun() {move (3,5);}

void f1() { A::show();}

private:

int x, y;

};

void main()

{

A e(1,2);

e.show();

B d(3,4,5,6);

d.fun();

d.show();

d.f1();

}

正确答案:

(1,2)

5,6

(6,9)

6

以下程序执行结果是________________。

#include <iostream.h>

class A

{

public:

A(int i, int j) {a = i; b = j;}

void move (int x, int y) {a += x; b += y;}

void show() { cout << “(“ << a << “,” << b << “)” << endl;}

private:

int a, b;

};

class B: public A

{

public:

B(int i, int j, int k, int l):A(i, j)

{ x =k; y = l; }

void show()

{ cout << x << “,” << y << endl;}

void fun() {move (3,5);}

void f1() { A::show();}

private:

int x, y;

};

void main()

{

A e(1,2);

e.show();

B d(3,4,5,6);

d.fun();

d.A::show();

d.B::show();

d.f1();

}

正确答案:

(1,2)

(6,9)

5,6

(6,9)

7

以下程序执行结果是________________

# include <iostream.h>

class A {

int a;

public:

A(int aa=0) { a=aa; }

~A() { cout <<"Destructor A!"<<a<<endl; }

};

class B:public A {

int b;

public:

B(int aa=0,int bb=0):A(aa) { b=bb; }

~B() { cout <<"Destructor B!"<<b<<endl; }

};

void main() {

B x(5),y(6,7); // 后定义的变量将先被释放

}

正确答案:

Destructor B! 7

Destructor A! 6

Destructor B! 0

Destructor A! 5

8

以下程序运行的结果_________。

#include “iostream.h”

class CSample

{

private:

int i;

public:

CSample();

CSample(int val);

void Display();

~CSample();

};

CSample::CSample()

{

cout << “Constructor1” << endl;

i=0;

}

CSample:: CSample(int val)

{

cout << “Constructor2” << endl;

i=val;

}

void CSample::Display()

{

cout << ”i=” << i << endl;

}

CSample::~CSample()

{

cout << “Destructor” << endl;

}

void main()

{

CSample a, b(10);

a.Display();

b.Display();

}

正确答案:

Constructor1

Constructor2

i=0

i=10

Destructor

Destructor

9

以下程序执行的结果是_____________________。

#include <iostream.h>

class B

{

int x,y;

public:

B() {x = y = 0; cout << “Constructor1” << endl;}

B(int i) {x = i; y = 0; cout << “Constructor2” << endl;}

B(int i ,int j) {x = i; y = j; cout << “Constructor3” << endl;}

~B() {cout << “Destructor” << endl;}

void print() {cout << “x=” << x << “,y=” << y << endl;}

};

void main()

{

B *ptr;

ptr = new B[3];

ptr[0] = B();

ptr[1] = B(5);

ptr[2] = B(2,3);

for (int i=0;i<3;i++)

ptr[i].print();

delete[] ptr;

}

正确答案:

Constructor1

Constructor1

Constructor1

Constructor1

Destructor

Constructor2

Destructor

Constructor3

Destructor

x=0,y=0

x=5,y=0

x=2,y=3

Destructor

Destructor

Destructor

10

以下程序执行的结果是_____________________。

class B{

int x,y;

public:

B() {x = y = 0; cout << "Constructor1" << endl;}

B(int i) {x = i; y = 0; cout << "Constructor2" << endl;}

B(int i ,int j) {x = i; y = j; cout << "Constructor3" << endl;}

~B() {cout << "Destructor" << endl;}

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

};

void main(){

B *ptr;

ptr = new B[2];

ptr[0] = B();

ptr[1] = B(5);

delete[] ptr;

}

正确答案:

Constructor1

Constructor1

Constructor1

Destructor

Constructor2

Destructor

Destructor

Destructor

11

以下程序执行结果是_____________。

#include <iostream.h>

class A

{

public:

A(int i=0) {m = i; cout << "Constructor" << m << endl; }

void set(int i) {m = i;}

void print() const {cout << m << endl;}

~A() {cout << "Destructor" << m << endl;}

private:

int m;

};

void fun(const A &c)

{

c.print();

}

void main()

{

fun(5);

}

正确答案:

Constructor5

5

Destructor5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值