by John
Kopp
Introduction Welcome to About.com's
free tutorial on C++ programming. This lesson covers abstract
data types and pure virtual functions. An abstract data type
is a class
that is meant to serve only as an interface for derived
classes; it cannot be instantiated.
They serve as base
classes to other classes that will be instantiated into
objects. In earlier
lessons, we saw the role of virtual functions in allowing
polymorphism
in our code. In C++, a pure virtual function is not only
intended to be overridden in base classes, it must be. Pure
virtual functions provide a way to force derived
classes to implement a particular interface.
Let's return to our Vehicle and Car classes of the last few
lessons. Conceptually, the Vehicle class should be an abstract
data type. It's possible to drive a Car, accelerate a Car,
decelerate a Car, but not a Vehicle. A vehicle is an
abstraction, or concept. A Car can be a concrete object that
we can use to achieve some end in either our software world or
the physical world. If a vehicle can't be concrete, if it
can't be instantiated, what's its purpose? It serves as a
model of the abilities and properties a Car will have. A
vehicle can provide implementations for some of these
abilities through its non-virtual
and non-pure
virtual methods. It can also provide only an interface for
other methods through the use of pure virtual functions. The
interface of a method refers to the number and type of
arguments and to the return type; it is what the outside
world, other classes need to know to use the function. It
describes what they must provide and what to expect back. Next
page > Abstract
Data Types > Page 1,
2,
3
|