1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
// �bung PG 2, Teil 3
// inhomogeneous list for Shape-Objects
// Author: Prinz / Kirch-Prinz / Weber
// Date: 26.05.05
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::left;
#include <typeinfo>
#include <list> // 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;
}
|