// Übung PG 2, Teil 3 // inhomogeneous list for Shape-Objects // Author: Prinz / Kirch-Prinz / Weber // Date: 26.05.05 #include using std::cout; using std::endl; using std::cin; using std::left; #include #include // class-Template list< T > // T is type of list elements #include "shape.h" typedef std::list< Shape* > ShapePtrList; void printShapeList( const ShapePtrList& spl ); int main() { cout << "\n\t *** Eine Liste geometrischer Figuren ***\n" << endl; ShapePtrList myShapes; printShapeList( myShapes ); cout << "\nElemente in die Liste einfuegen: " << endl; // append at end of list: myShapes.push_back( new Line( Point( 0, 0 ), Point( 2, 2 ) ) ); myShapes.push_back( new Rectangle( Point( -1, -1 ), 2, 2 ) ); // insert ellipse at front of list: myShapes.push_front( new Ellipse( Point( 0, 0 ), 3, 1) ); Point vertices[] = { Point( 0, -3 ), Point( -3, 0 ), Point( 0, 3 ), Point( 3, 0 ) }; Shape* ptr = new Polygon( vertices, 4 ); // a rombus // insert polygon as second element: myShapes.insert( ++myShapes.begin(), ptr ); // a circle as last-but-one element: ShapePtrList::iterator pos = // position of circle myShapes.insert( --myShapes.end(), new Circle( Point( 0, 0 ), 5 ) ); ( *pos )->scale( 0.7 ); // make this element smaller cout << "Anzahl Elemente in der Liste: " << myShapes.size() << endl; printShapeList( myShapes ); // print list cout << "Figur vor dem Kreis loeschen ... " << endl; myShapes.erase( --pos ); cout << "und die zweite Figur (das Polygon) verschieben: " << endl; pos = myShapes.begin(); ptr = *( ++pos ); // second element = pointer to polygon ptr->move( 0, 3 ); // move upwards cout << "Die veraenderte Liste: " << endl; printShapeList( myShapes ); // print list return 0; } void printShapeList( const ShapePtrList& spl ) { if( spl.empty() ) { cout << "Die Liste ist leer!" << endl; return; } ShapePtrList::const_iterator pos = spl.begin(); for( ; pos != spl.end(); ++pos ) { cout.width(20); cout << left << typeid( **pos ).name(); cout << ( *pos )->toString() << endl; } cout << endl; }