// ( c8.C ) // コンストラクタの検討−継承クラスを含む場合 // #include // 基底クラスにコンストラクタがない場合 class Base1 { protected: int color ; } ; class Point1: public Base1 { public: Point1( int c=2 ) { color = c ; } void show() { cout << "Color = " << color << "\n" ; } } ; // 基底クラスに void 型でないコンストラクタがある場合 class Base2 { protected: int color ; public: Base2( int c ) { color = c ; } } ; class Point2: public Base2 { public: Point2( int c ):Base2(c) {} // これは有効! void show() { cout << "Color = " << color << "\n" ; } // Point2( int c ) { color = c ; } // これはエラ−になる! } ; // 基底クラスに void 型のコンストラクタがある場合 class Base3 { protected: int color ; public: // void 型コンストラクタを用意した Base3(void) {} // Base3( int c=0 ) { color=c; } とし、Base3(void) を取っても同様な効果がある。 class Point3: public Base3 { public: void show() { cout << "Color = " << color << "\n" ; } Point3( int c ) { color = c ; } // 注目! } ; main() { Point1 a(10) ; a.show() ; Point2 b(20) ; b.show() ; Point3 c(30) ; c.show() ; }