1 /* File : example.h */ 2 #include <vector> 3 4 class Shape { 5 public: Shape()6 Shape() { 7 nshapes++; 8 } ~Shape()9 virtual ~Shape() { 10 nshapes--; 11 } 12 double x, y; 13 void move(double dx, double dy); 14 virtual double area(void) = 0; 15 virtual double perimeter(void) = 0; 16 static int nshapes; 17 }; 18 19 class Circle : public Shape { 20 private: 21 double radius; 22 public: Circle(double r)23 Circle(double r) : radius(r) { } 24 virtual double area(void); 25 virtual double perimeter(void); 26 }; 27 28 class Square : public Shape { 29 private: 30 double width; 31 public: Square(double w)32 Square(double w) : width(w) { } 33 virtual double area(void); 34 virtual double perimeter(void); 35 }; 36 37 38 Circle* createCircle(double w); // this method creates a new object 39 Square* createSquare(double w); // this method creates a new object 40 41 class ShapeOwner { 42 private: 43 std::vector<Shape*> shapes; 44 ShapeOwner(const ShapeOwner&); // no copying 45 ShapeOwner& operator=(const ShapeOwner&); // no copying 46 public: 47 ShapeOwner(); 48 ~ShapeOwner(); 49 void add(Shape* ptr); // this method takes ownership of the object 50 Shape* get(int idx); // this pointer is still owned by the class (assessor) 51 Shape* remove(int idx); // this method returns memory which must be deleted 52 }; 53 54 55