Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Abstract Classes and Concrete Classes

In C++ an abstract class  is one which defines an interface, but does not necessarily provide implementations for all its member functions. An abstract class is meant to be used as the base class from which other classes are derived. The derived class is expected to provide implementations for the member functions that are not implemented in the base class. A derived class that implements all the missing functionality is called a concrete class .

E.g., the GraphicalObject class defined in Programs gif and gif is an abstract class. In particular, no implementation is given for the virtual member function Draw. The fact that no implementation is given is indicated by the =0 attached to the Draw function prototype in the class definition (Program gif, line 19). Recall that an object carries a pointer to the appropriate routine for every virtual member function it supports. The =0 says that the pointer for the Draw function is not defined in the GraphicalObject class and, therefore, it must be defined in a derived class.

A virtual member function for which no implementation is given is called a pure virtual function . If a C++ class contains a pure virtual function, it is an abstract class. In C++ it is not possible to instantiate an abstract class. E.g., the following declaration is illegal:

GraphicalObject g (Point (0,0)); // Wrong.
If we were allowed to declare g in this way, then we could attempt to invoke the non-existent member function g.Draw().

In fact, there is a second reason why the declaration of g is an error: The GraphicalObject constructor is not public (see Program gif, lines 15-16). So, even if the GraphicalObject class was a concrete class, we are still prevented from instantiating it. Since the constructor is protected, it can be called by a derived class. Therefore, it is possible to instantiate Circle, Rectangle and Square.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.