previous | start | next

Usefulness of Dynamic Dispatch

Promotes reuse of code.

Example.

class Shape {
public: 

   virtual bool contains(Point p) const = 0; // Pure virtual
   virtual void draw() = 0;                  // Pure virtual
   ...
};

class Circle: public Shape {
public:
   bool contains(Point p) const;
   void draw();
   void drawCircle();
   ...
};

class Rectangle: public Shape {
public:
   bool contains(Point p) const;
   void draw();
   void drawRectangle();
   ...
};

class Polygon: public Shape {
public:
   bool contains(Point p) const = 0;
   void draw();
   void drawPolygon();
   ...
};



previous | start | next