/************************************************************************************** *** FILE: typeid.cpp *** DATE: 03/07/2004 *** AUTHOR: Daniel Dementiev *** GOAL: This program illustrates the usag eof the typeid() operator. *** To compile from the command line use: *** C:\> cl typeid.cpp /EHsc /GR *** for Visual C++ command line compiler or *** C:\> bcc32 typeid.cpp *** for Borland C++ Builder command line compiler. *** Hierarchy of the classes is: *** Figure *** ^ *** ---------------------------- *** ^ ^ *** Circle Rect *** ^ *** Square **************************************************************************************/ #include #include #include using namespace std; class Figure { protected: int x, y; public: Figure() : x(0), y(0) {}; Figure(int X, int Y) : x(X), y(Y) {}; virtual ~Figure() {}; int getX() { return x; }; int getY() { return y; }; void MoveTo(int X, int Y) { x=X; y=Y; }; // virtual methdos - common interface of all Figure children virtual int getWidth() = 0; virtual int getHeight() = 0; }; class Circle : public Figure { private: int radius; public: Circle(void) : radius(10) {}; Circle(int x, int y, int r) : Figure(x, y), radius(r) {}; int getWidth() { return 2*radius; }; int getHeight() { return 2*radius; }; }; class Rect : public Figure { private: int width, height; public: Rect(void) : width(10), height(5) {}; Rect(int x, int y, int w, int h) : Figure(x, y), width(w), height(h) {}; int getWidth() { return width; }; int getHeight() { return height; }; }; class Square : public Rect { public: Square(void) : Rect(0, 0, 5, 5) {}; Square(int x, int y, int s) : Rect(x, y, s, s) {}; }; void main() { vector figs; int i; const int w = 600; const int h = 400; int r, s, rw, rh; for(i=0;i<25;i++) switch( rand()%4 ){ case 0: r = rand() % 40 + 3; figs.push_back( new Circle( rand()%(w-2*r)+r, rand()%(h-2*r)+r, r ) ); break; case 1: s = rand() % 60 + 5; figs.push_back( new Square( rand()%(w-s)+s/2, rand()%(h-s)+s/2, s ) ); break; case 2: rw = rand() % 60 + 5; rh = rand() % 30 + 3; figs.push_back( new Rect( rand()%(w-rw)+rw/2, rand()%(h-rh)+rh/2, rw, rh ) ); break; } for(i=0;i