UMBC CMSC 202 CSEE | 202 | current 202

Shapes - a dynamic binding example

The Inheritance Heirarchy

Operations:

The Shape Class

class Shape { public: virtual void Draw ( ) const = 0; // pure virtual method virtual void Error ( ) const; // virtual method void ObjectID ( ) const; // non-virtual method }; void Shape::Error ( ) const { cerr << "Shape error" << endl; } void Shape::ObjectID ( ) const { cout << "A shape" << endl; }

Draw ( )

Error ( )

ObjectId( )

The Circle Class

class Circle : public Shape { public: virtual void Draw( ) const; // method for drawing a circle virtual void Error ( ) const; // overriding Shape::Error( ) }; void Circle::Draw ( ) const { // code for drawing a circle } void Circle::Error ( ) const { cerr << "Circle Error" << endl; }

The Rectangle Class

class Rectangle : public Shape { public: virtual void Draw( ) const; // method for drawing a rectangle virtual void Error ( ) const; // overriding Shape::Error( ) }; void Rectangle::Draw ( ) const { // code for drawing a rectangle } void Rectangle::Error ( ) const { cerr << "Rectangle Error" << endl; }

Back to Dynamic Binding

Now, consider these pointers Shape *pShape; // static type = "pointer to Shape" Circle *pCircle = new Circle; // static type = "pointer to Circle" Rectangle *pRectangle = new Rectangle; // static type = "pointer to Rectangle" Each pointer has a static type based on the way it's declared. The static type determines which member functions can be called.
Each pointer has a dynamic type based on the type of object to which it currently points. The dynamic type determines which class' implementation of the member function will be used. pShape = pCircle; // pShape's dynamic type is "pointer to Circle" pShape->Draw( ); // calls Circle::Draw( ) and draws a Circle pShape = pRectangle; // pShape's dynamic type is now "pointer to Rectangle" pShape->Draw ( ); // calls Rectangle::Draw( ) and draws a Rectangle // an array of Shape (Base class) pointers Shape *shapes[3]; shapes[0] = new Circle; shapes[1] = new Rectangle; shapes[2] = new Triangle; for (int s = 0; s < 3; s++) shapes[s]->Draw( );


Last Modified: Monday, 28-Aug-2006 10:16:04 EDT