// ( o6.C ) // オブジェクトを管理するデ−タ集合 $ CC -o o6 o6.o o6s.o -O -v // #include #include "o6.h" // 汎用線形リスト部をインクル−ドする class Vector { // デ−タ集合は汎用線形リストで、単方向リストの格納 int x,y ; // 要素は void 型ポインタとする。void 型ポインタは、 public: // ポインタなら何でも格納できてしまう。 Vector( int a=0,int b=0 ) { x = a ; y = b ; } int get_x() { return x ; } } ; class Base { public: virtual int get_id() = 0 ; } ; class Point: public Base { Vector pt ; public: Point( int a=0,int b=0 ):pt(a,b) {} int get_id() { return 1 ; } // Point のID=1 int get_px() { return pt.get_x() ; } } ; class Line: public Base { Point p1,p2 ; public: Line( int x1,int y1,int x2,int y2 ):p1(x1,y1),p2(x2,y2) {} int get_id() { return 2 ; } int get_sx() { return p1.get_px() ; } // Line のID=2 } ; class EList: public List { // 既存クラスを拡張するということ public: void Report() ; } ; void EList::Report() { Node *Current = SNode ; while ( Current->Next != NULL ) { Base* temp = (Base*)Current->Item ; if ( temp->get_id() == 1 ) { // Point entity Point* vtemp = (Point*)temp ; cout << vtemp->get_px() << "\n" ; } else // Line entity cout << ((Line*)temp)->get_sx() << "\n" ; Current = Current->Next ; } ; } main() { EList entity ; entity.Add( new Point( 2,2 ) ) ; Point* temp = new Point( 1,1 ) ; entity.Add( temp ) ; entity.Del( temp ) ; entity.Add( new Point( 3,3 ) ) ; entity.Add( new Line( 10,10,20,20 ) ) ; entity.Report() ; delete temp ; // 結果 : 2 3 10 }