1 /*! \file example.h 2 This file provides a simple set of Shape classes. */ 3 4 /*! Base class for all shapes. 5 \author Bob 6 */ 7 class Shape { 8 public: 9 /*! Default constructor for creating a Shape */ Shape()10 Shape() { 11 nshapes++; 12 } 13 /*! Destructor for destroying a Shape */ ~Shape()14 virtual ~Shape() { 15 nshapes--; 16 } 17 double x; /*!< x co-ordinate */ 18 double y; /*!< y co-ordinate */ 19 void move(double dx, double dy); /*!< Move a shape to a new co-ordinate 20 \param dx x co-ordinate 21 \param dy y co-ordinate */ 22 virtual double area() = 0; /*!< \return the area */ 23 virtual double perimeter() = 0; /*!< \return the perimeter */ 24 static int nshapes; /*!< Number of shapes currently in existence */ 25 }; 26 27 /*! A class for representing a circle. 28 \author Jack 29 */ 30 class Circle : public Shape { 31 private: 32 double radius; 33 public: 34 /*! Construct a circle 35 * \param r radius of the circle */ 36 Circle(double r); 37 /*! Calculate the area of the circle 38 * \return calculated area */ 39 virtual double area(); 40 /*! Calculate the perimeter of the circle 41 * \return calculated perimeter of the circle */ 42 virtual double perimeter(); 43 }; 44 45 /// A class for representing a square. 46 class Square : public Shape { 47 private: 48 double width; 49 public: 50 /** Construct a square 51 * \param w width of the square */ 52 Square(double w); 53 /** Calculate the area of the square 54 * \return calculated area */ 55 virtual double area(); 56 /** Calculate the perimeter of the square 57 * \return calculated perimeter of the square */ 58 virtual double perimeter(); 59 }; 60 61 /// A class for representing a rectangle, templated on the type for the rectangle dimensions 62 template<typename T> 63 class Rectangle : public Shape { 64 private: 65 T height; 66 T width; 67 public: 68 /** Construct a rectangle 69 * \param h height of the rectangle 70 * \param w width of the rectangle */ Rectangle(T h,T w)71 Rectangle(T h, T w) : height(h), width(w) {} 72 /** Calculate the area of the rectangle 73 * \return calculated area */ area()74 virtual double area() { return width*height; } 75 /** Calculate the perimeter of the rectangle 76 * \return calculated perimeter of the rectangle */ perimeter()77 virtual double perimeter() { return 2*height + 2*width; } 78 }; 79 80 81 /*! Factory function for creating a square 82 * \param r width of the square 83 * \return a fully constructed square */ 84 Square MakeSquare(double r); 85 86 /*! Factory function for creating a circle 87 * \param w radius of the circle 88 * \return a fully constructed circle */ 89 Circle MakeCircle(double w); 90 91 /*! Factory function for creating a rectangle 92 * \param h height of the rectangle 93 * \param w width of the rectangle 94 * \return a fully constructed rectangle */ 95 template<typename T> MakeRectangle(T h,T w)96Rectangle<T> MakeRectangle(T h, T w) { 97 return Rectangle<T>(h, w); 98 } 99 100 101 102 /*! Total number of circles ever created */ 103 extern int NumCircles; 104 105 /// Total number of squares ever created 106 extern int NumSquares; 107 108