当前位置:首页 > 程序设计教程(机械工业出版社)课后习题答案 第8章 继承――派生类
第8章 继承――派生类
1、在C++中,protected类成员访问控制有什么作用?
答:C++中引进protected成员保护控制,缓解了数据封装与继承的矛盾。在基类中声明为
protected的成员可以被派生类使用,但不能被基类的实例用户使用,这样能够对修改基类的内部实现所造成的影响范围(只影响子类)进行控制。protected成员保护控制的引进使得类有两种接口:与实例用户的接口和与派生类用户的接口。
2、在C++中,三种继承方式各有什么作用?
答:类的继承方式决定了派生类的对象和派生类的派生类对基类成员的访问限制。public继
承方式使得基类的public成员可以被派生类的对象访问,它可以实现类之间的子类型关系;protected继承使得基类的public成员不能被派生类的对象访问,但可以被派生类的派生类访问;private继承使得基类的public成员既不能被派生类的对象访问,也不能被派生类的派生类访问。protected和private继承主要用于实现上的继承,即纯粹为了代码复用。
3、在多继承中,什么情况下会出现二义性?怎样消除二义性?
答:在多继承中会出现两个问题:名冲突和重复继承。在多继承中,当多个基类中包含同名
的成员时,它们在派生类中就会出现名冲突问题;在多继承中,如果直接基类有公共的基类,就会出现重复继承,这样,公共基类中的数据成员在多继承的派生类中就有多个拷贝。在C++中,解决名冲突的方法是用基类名受限;解决重复继承问题的手段是采用虚基类。
4、写出下面程序的运行结果:
#include
A() { cout << \
A(const A&) { cout << \ ~A() { cout << \};
class B
{ int x,y; public:
B() { cout << \
B(const B&) { cout << \ ~B() { cout << \};
class C: public B { int z; A a; public:
C() { cout << \
C(const C&) { cout << \
~C() { cout << \};
void func1(C x)
{ cout << \}
void func2(C &x)
{ cout << \}
int main()
{ cout << \ C c;
cout << \ func1(c);
cout << \ func2(c);
cout << \ return 0; }
答:------Section 1------
in B's default constructor in A's default constructor in C's default constructor ------Section 2------ in B's default constructor in A's default constructor in C's copy constructor In func1 in C's destructor in A's destructor in B's destructor ------Section 3------ In func2
------Section 4------ in C's destructor in A's destructor in B's destructor
5、 写出下面程序的运行结果:
#include
{ int x,y; public:
A() { cout << \
A(const A&) { cout << \ ~A() { cout << \ virtual void f() { cout << \ void g() { cout << \ void h() { f(); g(); } };
class B: public A { int z; public:
B() { cout << \
B(const B&) { cout << \ ~B() { cout << \ void f() { cout << \ void g() { cout << \};
void func1(A x) { x.f(); x.g(); x.h(); }
void func2(A &x) { x.f(); x.g(); x.h(); }
int main()
{ cout << \ A a;
A *p=new B;
cout << \ func1(a);
cout << \ func1(*p);
cout << \ func2(a);
cout << \ func2(*p);
cout << \ delete p;
cout << \ return 0; }
答:------Section 1------
in A's default constructor in A's f
in A's default constructor in A's f
in B's default constructor ------Section 2------ in A's copy constructor in A's f in A's f in A's g in A's f in A's g in A's destructor ------Section 3------ in A's copy constructor
in A's f in A's f in A's g in A's f in A's g in A's destructor ------Section 4------ in A's f in A's g in A's f in A's g
------Section 5------ in B's f in A's g in B's f in A's g
------Section 6------ in A's destructor ------Section 7------ in A's destructor
6、 利用习题6.8的第14题中的时间类Time,定义一个带时区的时间类ExtTime。除了构
造函数和时间调整函数外,ExtTime的其它功能与Time类似。 答:
#include
class Time {
public: Time();
Time(int h,int m,int s);
void set(int h, int m, int s); void increment();
void display() const;
bool equal(Time &other_time) const;
bool less_than(Time &other_time) const; protected: int hour; int minute; int second; };
enum TimeZone { W12=-12,W11,W10,W9,W8,W7,W6,W5,W4,W3,W2,W1, GMT,
E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12};
class ExtTime: public Time {
共分享92篇相关文档