diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h')
| -rw-r--r-- | Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h b/Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h new file mode 100644 index 0000000..2a4698f --- /dev/null +++ b/Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h @@ -0,0 +1,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
\ No newline at end of file |
