#include #include #include using namespace std; class Shape { public: Shape() {}; void setColor() { cout << " Shape::setColor() called "<< endl; } virtual void Draw() =0; // declare as pure virtual //.. }; class Rectangle : public Shape { public: int ncorners; Rectangle () { ncorners = 4;}; void Draw() { cout << " Rectangle::Draw() called "<< endl; } void ShowCorners() { cout << " Rectangle::ShowCorners() called "<< ncorners << endl; } }; class Oval : public Shape { public: void Draw() { cout << " Oval::Draw() called "<< endl; } }; class RoundRect : public Shape { public: void Draw() { cout << " RoundRect::Draw() called "<< endl; } }; void gimeSomeShapes( vector &vs, int nshapes ) { // create concrete shapes randomly and add to vector for ( int i=0; i(rand()*3./(RAND_MAX+1.0)); // Zufallszahlen [0..3) switch (irand ) { case 0: vs.push_back( new Rectangle()); break; case 1: vs.push_back( new Oval()); break; case 2: vs.push_back( new RoundRect()); break; } } } int main () { vector vecshape; // vector with base-class pointers gimeSomeShapes( vecshape, 10 ); // erzeuge shapes for ( int i=0; isetColor( ); vecshape[i]->Draw( ); vecshape[i]->ShowCorners( ); // won't work } }