From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h (limited to 'Bachelor/Prog2/Z-Uebung/Teil4/shapeList.h') 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 +using std::cerr; +using std::left; + +#include // exit() +#include // 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 -- cgit v1.2.3