// ( o8.C ) // デ−タ集合を個々のオブジェクト自身が持つ // #include // デ−タを定義するだけで配列のデ−タベ−スに // そのデ−タを入れてしまう。 class Array { static void* array[100] ; // この2つはスタティック変数として static int i ; // 定義しなければならない public: Array() { i=0; } void in( void* a ) { array[i++] = a ; } void* out( int i ) { return array[i] ; } int& get_i() { return i ; } } ; class Base { // Point, Line の基底クラス protected: static Array ent ; // スタティックとして定義する public: virtual void print() = 0 ; // 仮想関数 void print_all() { int i = ent.get_i() ; for ( int j=0;jprint() ; } } ; class Point: public Base { // Base の派生クラスは、自動的にデ−タベ−ス int x,y ; // が定義されて、デ−タが更新される public: Point( int a=0,int b=0 ) { x=a ; y=b ; ent.in(this) ; } void print() { cout << "point\n" ; } } ; class Line: public Base { Point p1,p2 ; public: Line( int x1,int y1,int x2,int y2 ):p1(x1,y1),p2(x2,y2) { ent.in(this) ; } void print() { cout << "line\n" ; } } ; main() { Point A(1,1) ; // あまりこの様にデ−タを管理する例は見当たらないが、 Point B(2,2) ; // どんなものだろう。デ−タを外で管理するよりも、こ Line C(4,4,4,4) ; // の方がオブジェクトを扱っているという感じがするの Point D(3,3) ; // でないか。欠点はデ−タを管理する型を、あらかじめ // 決めてしまうことにある。 D.print_all() ; } /* [結果] point point point point line point */