派生类的构造函数
封闭派生类对象的构造函数的执行顺序
派生类的析构函数
派生类析构函数的调用顺序
代码实例
#include <bits/stdc++.h>using namespace std;class bug{ private: int nLegs; int color; public: int type; bug(int l, int c, int t); void print_bug(void);};bug::bug(int l, int c, int t) : nLegs(l), color(c), type(t){};void bug::print_bug(void){ cout << "Legs: " << nLegs << endl; cout << "Color: " << color << endl; cout << "Type: " << type << endl;}//-----------------------------------------------------------------------------------------------class flybug : public bug{ private: int nWings; public: flybug(int l, int c, int t, int w); void print_flybug(void);};flybug::flybug(int l, int c, int t, int w) : bug(l, c, t), nWings(w){};void flybug::print_flybug(void){ print_bug(); cout << "Wings: " << nWings << endl;}//----------------------------------------------------------------------------------------------int main(void){ flybug fly(8, 1, 2, 4); fly.type = 3; fly.print_bug(); fly.print_flybug(); return 0;}/*Legs: 8Color: 1Type: 3Legs: 8Color: 1Type: 3Wings: 4*/
#include <bits/stdc++.h>using namespace std;class c{ public: c(void) { cout << "C Constructed" << endl; }; ~c(void) { cout << "C Destructed" << endl; };};class base{ public: base(void) { cout << "Base Constructed" << endl; }; ~base(void) { cout << "Base Destructed" << endl; };};class derived : public base{ private: c C; public: derived(void) { cout << "Derived Constructed" << endl; } ~derived(void) { cout << "Derived Destructed" << endl; };};int main(void){ derived d; return 0;}/*Base ConstructedC ConstructedDerived ConstructedDerived DestructedC DestructedBase Destructed*/