![]() |
![]() |
TOP > C++ > クラスの継承(2) コンストラクタ |
![]() |
![]() |
基底クラスのコンストラクタに引数を渡す
継承クラスのオブジェクトを生成時、基底クラスのコンストラクタを呼び引数を渡す事ができる。
継承クラスのコンストラクタ内で以下のように記述する
コンストラクタ名(...) : 基底クラスコンストラクタ名(...);
基底クラスのコンストラクタに引数を渡す例
#include <iostream> using namespace std; class Parent { public: Parent(int a) { cout << "Parent Constructor a = " << a << endl; } ~Parent() { cout << "Parent Destructor " << endl; } }; class Child : public Parent { public: Child(int a) : Parent(a) { cout << "Child Constructor a = " << a << endl; } ~Child() { cout << "Child Destructor " << endl; } }; int main(int argc, char** argv) { Child i_child(3); return(0); } |
![]() |
![]() |
Copyright 2007 ためになるホームページ All Rights Reserved. |