Monday, November 29, 2010

Abstract Classes and Pure Virtual Functions

Think of the shape class in the multshap program in Chapter 9. We’ll never make an object of
the shape class; we’ll only make specific shapes such as circles and triangles. When we will
never want to instantiate objects of a base class, we call it an abstract class. Such a class exists
only to act as a parent of derived classes that will be used to instantiate objects. It may also
provide an interface for the class hierarchy.
How can we make it clear to someone using our family of classes that we don’t want anyone to
instantiate objects of the base class? We could just say this in the documentation, and count on
the users of the class to remember it, but of course it’s much better to write our classes so that
such instantiation is impossible. How can we can do that? By placing at least one pure virtual
function in the base class. A pure virtual function is one with the expression =0 added to the
declaration

1 comment:

  1. Thank you for posting such a great blog. I found your website perfect for my needs. Read About STD Clinic

    ReplyDelete