Contents Up << >>

What is a "pure virtual" member function?

A member function of an ABC that you can implement only in a derived class.

Some member functions exist in concept, but can't have any actual defn. E.g., suppose I asked you to draw a Shape at location (x,y) that has size 7. You'd ask me, "What kind of shape should I draw?" (circles, squares, hexagons, etc, are drawn differently). In C++, we indicate the existence of the " draw()" method, but we recognize it can (logically) be defined only in subclasses:

  	class Shape {
	public:
	  virtual void draw() const = 0;
	  //...                     ^^^--- "= 0" means it is "pure virtual"
	};
This pure virtual function makes "Shape" an ABC. If you want, you can think of the "= 0" syntax as if the code were at the NULL pointer. Thus "Shape" promises a service to its users, yet Shape isn't able to provide any code to fulfill that promise. This ensures any actual object created from a [concrete] class derived of Shape will have the indicated member fn, even though the base class doesn't have enough information to actually define it yet.