// ( c16.C ) // クラス階層の設計−その3 $ cpc c16;CC -o c16 c16.o o6s.o -O -v;c16 // #include // 【仮想関数部と共通デ−タ部を多重継承させる場合】 #include "o6.h" class Base { // 仮想関数を実現するための抽象クラス public: virtual void show_color() = 0 ; } ; class Atr { // 共通クラスとしての抽象クラス protected: int color ; } ; // 注意: public Atr,public Base { の順番ではダメ class Point1: public Base,public Atr { public: Point1(void) { color = 1 ; } void show_color() { cout << "Point1 Color = " << color << endl ; } } ; class Point2: public Base,public Atr { public: Point2() { color = 2 ; } void show_color() { cout << "Point2 Color = " << color << endl ; } } ; class Coor1: public Point1 { int x,y ; public: Coor1() { x = y = 1 ; color = 3 ; } void show_color() { cout << "Coor1 Color = " << color << endl ; } } ; class EList: public List { // List というのは、ここではインスタンスの単 public: // なる入れ物と思って頂いて構わない。 void Report() ; } ; void EList::Report() { Node *Current = SNode ; while ( Current->Next != NULL ) { ((Base*)(Current->Item))->show_color() ; Current = Current->Next ; } ; } // 仮想関数を実現するための抽象クラスと、共通クラスとしての抽 main() // 象クラスの2つを見ると、どっちが上のクラスということはない。 { // それで派生クラスを作成する際には、どっちも同時に継承してし EList entity ; // まおうという発想である。 Point1* p1 = new Point1() ; entity.Add( p1 ) ; Point2* p2 = new Point2() ; entity.Add( p2 ) ; Coor1* c1 = new Coor1() ; entity.Add( c1 ) ; entity.Report() ; delete p1 ; delete p2 ; delete c1 ; }