派生类

    • Inherit members (attributes and functions) from one class
      • Base class (parent)
      • Derived class (child)
    • C++ supports multiple inheritance and multilevel inheritance

    子类可以得到父类的成员

    1. class Base
    2. {
    3. public:
    4. int a;
    5. int b;
    6. };
    7. class Derived: public Base
    8. {
    9. public:
    10. int c;
    11. };
    12. class Derived: public Base1, public Base2
    13. {
    14. ...
    15. };

    Constructors

    • To instantiate a derived class object
      • Allocate memory
      • Derived constructor is invoked
        • Base object is constructed by a base constructor
        • Member initializer list initializes members
        • To execute the body of the derived constructor ```cpp class Derived: public Base { public: int c; Derived(int c): Base(c - 2, c - 1), c(c) { … } };

    ``` Destructors

    • The destructor of the derived class is invoked first, 子类的析构函数先执行
    • Then the destructor of the base class.