継承した場合、コンストラクタが呼ばれる順番
親から子へ。
親の上に子供が乗ってる絵が思い浮かべばその順番。このへんは予想通りかな。
ソースコード
#include <iostream> using namespace std; class Parent { public: Parent::Parent() { cout << "Parent Constructor." << endl; } Parent::~Parent() { cout << "Parent Destructor." << endl; } }; class Child : public Parent { public: Child::Child() { cout << "Child Constructor." << endl; } Child::~Child() { cout << "Child Destructor." << endl; } }; int main() { cout << "Main Start." << endl; Child c; cout << "hello. " << endl; }
実行結果
Main Start. Parent Constructor. Child Constructor. hello. Child Destructor. Parent Destructor.