summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h
blob: 2a4698f0d5beae3be679abe24f38137d0962f8bd (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
// shapeList.h

// Definition of classes ShapePtr and ShapePtrList

// inhomogeneous list for Shape-Objects 



#ifndef SHAPELIST_H

#define SHAPELIST_H



#include <iostream>

using std::cerr;

using std::left;



#include <cstdlib>  // exit()

#include <list>     // class-Template list< T >

                    // T is type of list elements

#include "shape.h"



class ShapePtr

{

private:

   Shape* ptr;

public:

   ShapePtr( Shape* p = NULL ) : ptr( p ) { }  // constructors

   ShapePtr( const ShapePtr& sp ) { ptr = sp->clone(); }



   ~ShapePtr() { delete ptr; }  // destructor



   ShapePtr& operator=( Shape* p )  // assignments

   {

      delete ptr;

      ptr = p->clone();

      return *this;

   }



   ShapePtr& operator=( ShapePtr& a )

   {

      delete ptr;

      ptr = a->clone();

      return *this;

   }



   Shape& operator*() const  // dereferencing

   {

      if( !ptr ) { 

         cerr << "ShapePtr::operator* : Kein Objekt!" << endl;

         exit( 100 );

      }

      return *ptr;

   }



   Shape* operator->() const  // member selection via pointer

   {

      if( !ptr ) { 

         cerr << "ShapePtr::operator-> : Kein Objekt!" << endl;

         exit( 101 );

      }

      return ptr;

   }



   operator Shape*() const { return ptr; }  // cast

};



class ShapePtrList : public std::list< ShapePtr >

{

public:

   void scale( double scalingFactor )

   {

      ShapePtrList::iterator pos;

      for( pos = begin(); pos != end(); ++pos )

         ( *pos )->scale( scalingFactor );

   }



   string toString() const

   {

      stringstream sstream;

      if( empty() )

         sstream << "Die Liste ist leer!"; 

      else {

         ShapePtrList::const_iterator pos;

         for( pos = begin(); pos != end(); ++pos ) {

            sstream.width( 20 );

            sstream << left << typeid( **pos ).name();

            sstream << ( *pos )->toString() << endl;

         }

      }

      return sstream.str();

   }

};



#endif