软件设计(II)教学课件:Chapter8 Inheritance.pptVIP

  • 0
  • 0
  • 约1.29万字
  • 约 32页
  • 2021-11-30 发布于安徽
  • 举报

软件设计(II)教学课件:Chapter8 Inheritance.ppt

* In principle, every member of a base class is inherited by a derived class Including public and protected members However, there are exceptions for constructor and destructor operator=() member (discuss later) Friends (discuss later) Since all these functions are class-specific §8.4 Constructors in Derived Classes * The constructors of a base class are not inherited How to initialize the data fields inherited from the base class? By calling base class constructors from the constructors of the derived classes Argument passing mechanism A (Except constructors ) B Constructor of Base * Calling Constructor of Base derived-constructor (type1 p1, type2 p2, …, typn pn, …): base1-constructor(p1), base2-constructor(p2){ //body of derived constructor; data1 = pn; } * No-Arg Constructor in Base Class A constructor in a derived class must always invoke a constructor in its base class. If a base constructor is not invoked explicitly, the base class’s no-arg constructor is invoked by default. For example, * Multiple Inheritance Base classes are constructed in the order in which they are declared Multilevel Inheritanc Constructors are executed in the order of inheritance Virtual Base Class Invoked before any non-virtual base classes If there are multiple virtual base classes, they are invoked in the order in which they are declared Order of Execution * Example More than one constructor/destructor to invoke B() ~B() C() ~C() ME() ~ME() class A {//…}; class X {//…}; class B: public A, virtual public X{//…}; class C: virtual public X{//…}; class ME: public B, public C{//…}; A() ~A() X() ~X() Invoking order of constructors: X, A, B, C, ME Invoking order of destructors: in reverse order Object Data Field The data field of a class is a an object * class alpha{ …}; class beta{ …}; class gamma{ …}; alpha a; beta b; …. }; How to initialize them? * Class objects can be initialized either in the constructor

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档