c++课后习题解答6-10 下载本文

内容发布更新时间 : 2024/6/16 6:52:59星期一 下面是文章的全部内容请认真阅读。

A.公有继承的公有成员 B.保护继承的公有成员 C.私有继承的公有成员 D.公有继承的保护成员 5.下列描述中,错误的是( D )。

A.基类的protected成员在public派生类中仍然是protected成员 B.基类的private成员在public派生类中是不可访问的 C.基类public成员在private派生类中是private成员 D.基类public成员在protected派生类中仍是public成员

6.派生类构造函数的成员初始化列表中,不能包含的初始化项是( B )。 A.基类的构造函数 B.基类的子对象 C.派生类的子对象 D.派生类自身的数据成员 7.下列关于子类型的描述中,错误的是( B )。 A.在公有继承下,派生类是基类的子类型

B.如果类A是类B的子类型,则类B也是类A的子类型 C.如果类A是类B的子类型,则类A的对象就是类B的对象 D.在公有继承下,派生类对象可以初始化基类的对象引用 8.下列关于多继承二义性的描述中,错误的是( D )。

A.一个派生类的多个基类中出现了同名成员时,派生类对同名成员的访问可能出现二

义性

B.一个派生类有多个基类,而这些基类又有一个共同的基类,派生类访问公共基类成

员时,可能出现二义性

C.解决二义性的方法是采用类名限定

D.基类和派生类中同时出现同名成员时,会产生二义性

8.3 填空题

1.继承的3种方式是 公有 、 私有 和 保护 。

2.如果类A继承了类B,则类A被称为 派生 类,类B被称为 基 类。

3.在保护继承方式下,基类的public成员成为派生类的 保护 成员,基类的protected成员成为派生类的 保护 成员。

4.当一个派生类中含有子对象时,该派生类的析构函数中应包含 直接基类 的析构函数、 子对象类的 析构函数和 自身类 的析构函数。

5.派生类的构造函数的成员初始化列表中可以包含的初始化项有 基类的构造函数 、 子对象类的构造函数 、 常数据成员 和 派生类其他非静态数据成员 。

8.4 分析下列程序的输出结果 1.

#include class A {

public:

A(int i,int j)

{ a1=i;a2=j; } void Move(int x,int y) { a1+=x;a2+=y; } void Print()

{ cout<<'('<

class B:private A {

public:

B(int i,int j,int k,int l):A(i,j) { b1=k;b2=l; }

21

void Print()

{ cout<

{ A::Print(); } void fun()

{ Move(5,8); } private: int b1,b2; };

void main() {

A a(11,12); a.Print();

B b(31,32,33,34); b.fun(); b.Print(); b.f(); }

答: (11,12) 33.34 (36,40)

2.

#include class A {

public:

void InitA(int i,int j) { a1=i;a2=j; } void Move(int x,int y) { a1+=x;a2+=y; } int Geta1()

{ return a1; } int Geta2()

{ return a2; } private: int a1,a2; };

class B:public A {

public:

void InitB(int i,int j,int k,int l) {

InitA(i,j); b1=k; b2=l; }

void Move(int x,int y) { b1+=x;b2+=y; } int Getb1()

{ return b1; } int Getb2()

{ return b2; } private: int b1,b2; };

class C:public B {

public:

void fun()

{ Move(10,15); }

22

}; void main() { C c;

c.InitB(11,12,13,14); c.fun();

cout<

答: 11,12,23,29

3.

#include class A { public: A(int i):a(i)

{ cout<<\ } ~A()

{ cout<<\ } void Print()

{ cout<

class B:public A { public:

B(int i=0,int j=0):A(i),a(j),b(i+j) { cout<<\ } ~B()

{ cout<<\ } void Print() {

A::Print();

cout<

23

void main() {

B b1(8),b2(12,15); b1.Print(); b2.Print(); }

答: A:constructor called. A:constructor called. B:Constructor called. A:constructor called. A:constructor called. B:Constructor called. 8 8,0 12 27,15

B:Destructor called. A:Destructor called. A:Destructor called. B:Destructor called. A:Destructor called. A:Destructor called.

4.

#include class A { public: A(int i)

{ cout<<\ } ~A()

{ cout<<\ } }; class B { public: B(int i)

{ cout<<\ } ~B()

{ cout<<\ } }; class C

24

{ public: C(int i)

{ cout<<\ } ~C()

{ cout<<\ } };

class D:public A,public B,public C { public:

D(int i,int j,int k,int l):A(i),B(j),C(k),a(l) { cout<<\ } ~D()

{ cout<<\ private: A a; };

void main() {

D d(3,4,5,6); }

答: Constructor in A. 3 Constructor in B. 4 Constructor in C. 5 Constructor in A. 6 Constructor in D. 6 Destructor in D. Destructor in A. Destructor in C. Destructor in B. Destructor in A.

8.5 编程题

1.按下列要求编程:按照右边图中所示的各类的关系,编程输出它们的信息。各类中的数据成员如下:

Person: char *name(姓名),*dept(系别) Student: char *grade(年级) Teacher: char *lesson(授课名)

Student Teacher(在职读研): char *major(专业方向)

Person Student Teacher Student Teacher 答: 编程如下:

25