// Test driver for Shape classes #include "Shape.h" #include // This should really be defined in a Shape.c source file int Shape::refs = 0; // Function to allocate Shapes on the heap and fill array with pointers void getShapes(Shape * list[], int count) { for (int i = 0; i < count; ++i) { switch(i % 5) { case 0: list[i] = new Circle(2.5); break; case 1: list[i] = new Rectangle(i+1.5, i+4); break; case 2: list[i] = new Triangle(i+3, i+4); break; case 3: list[i] = new Square(5); break; case 4: list[i] = new SquareHole(5); break; } } } // Pass a Circle by value void printCircle(Circle c) { cout << "In printCircle, c has area " << c.area() << " and there are now " << Shape::refCount() << " shapes" << endl; } int main() { Shape * shapes[5]; int n = sizeof(shapes)/sizeof(Shape *); getShapes(shapes, n); for (int i = 0; i < n; ++i) cout << "shapes[" << i << "] is a " << (*shapes[i]) << " with perimeter " << shapes[i]->perimeter() << " and area " << shapes[i]->area() << endl; shapes[0]->resizeBy(.5); shapes[1]->resizeBy(2); cout << "After resizing..." << endl; for (int i = 0; i < n; ++i) cout << "shapes[" << i << "] is a " << (*shapes[i]) << " with perimeter " << shapes[i]->perimeter() << endl; cout << "There are " << Shape::refCount() << " shapes" << endl; { Circle c1(5); cout << "Now there are " << Shape::refCount() << " shapes" << endl; printCircle(c1); { Circle c2(c1); cout << "Now there are " << Shape::refCount() << " shapes" << endl; } } cout << "Now there are " << Shape::refCount() << " shapes" << endl; for (int i = n-1; i > 0; --i) { cout << "Deleting shapes[" << i << "] ... "; delete shapes[i]; cout << "now there are " << Shape::refCount() << " shapes" << endl; } return 0; }