[Assignment #11 Inheritance and Polymorphism]

 

Given a class ShapeSet, which is a set of arbitrary shapes, complete this class. A main program will be given at the lab. Note that Shape and its child classes should work as polymorphism.

 

class ShapeSet {

Shape** shapes;

int    numShapes;

int    maxShapes; // maximum number of shape objects

public:

ShapeSet(int n); // initialize a ShapeSet where maxShape is n

ShapeSet operator+(Shape& s); //add a shape to ShapeSet.
double totalArea(); // total area of shapes in the set};

};

 

class Shape {

protected:

  int  type; // POINT, CIRCLE, RECTANGLE, or TRIANGLE

public:

int getType() const;

enum {POINT, CIRCLE, RECTANGLE, TRIANGLE};

virtual double area()=0;

};

 

class Point : public Shape {

double x, y;

public:
Point(double, double);
double area() { return 0.0;}

¡¦

};

 

class Rectangle : public Shape {

Point rightUpper, leftLower;

public:

Rectangle(const Point&, const Point&);

double area();

¡¦

}

 

class Circle : public Shape {

Point Center;

double radius;

public:

Circle(const Point&, double);

double area();

¡¦

};

 

class Triangle : public Shape {

Point p1, p2, p3;

public:

 Triangle(const Point&, const Point&, const Point&);

bool isContaining(const Point&);

double area();

 ¡¦

};

 

Oral Test: Lab. on May 31