派生类
- Inherit members (attributes and functions) from one class
- Base class (parent)
- Derived class (child)
- C++ supports multiple inheritance and multilevel inheritance
子类可以得到父类的成员
class Base
{
public:
int a;
int b;
};
class Derived: public Base
{
public:
int c;
};
class Derived: public Base1, public Base2
{
...
};
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.