Abstract Classes and Pure Virtual Functions

Before we can delve into the topic of abstract classes, we must first explore what a virtual function is. A virtual function is a function that, if inherited, must be overridden.

Let us examine exactly what this means. In a class, you may have one or more functions. If you inherit from that class you have the choice of either using the inherited function as it exists in the base class or overriding it.


You will see the terms override and overwrite used in different textbooks. They both refer to the same operation. That is why this text purposely uses both terms.

You create virtual functions by adding the word virtual to their declaration as shown in the following example.

virtual void funca();

With virtual functions, if you simply create an instance of the class they are in, then the function is treated as any other normal function. However, if the class is inherited, then the virtual function must be overwritten. The following code segment illustrates this.

class someclass {    public: virtual  void funca(); }; void someclass::funca() {   cout << "This is my function in the base class\n"; } class anotherclass {   public: void funca(); } void anotherclass::funca() {    cout << "This is in my derived class \n"; } int main() {   someclass a;   anotherclass b;     a.funca();    b.funca();    return 0; }// end of main 

As you can see, if you create an instance of this class, then you can use the function as its written; if you inherit from this class, then the derived class will have to write its own implementation. If you don’t override the function in the derived class, you will get a compiler error. Another way of looking at virtual functions is to realize that they have no effect on the class they are in. They only affect classes that inherit from that class. If the base class that contains the virtual function is instantiated, then the virtual function behaves just like any other ordinary function. However, if some class inherits from a base class that contains a virtual function, then the derived class will have to override the virtual function.

An abstract class is a class defining an interface only; used as a base class. An abstract class cannot be instantiated. It can only be inherited from. Declaring a member function of a class as a pure virtual function makes the class abstract and prevents creation of objects of the abstract class. Another way to say this is to state that an abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class is created by making any of its member functions a pure virtual function. The way you create a pure virtual function is simply by adding an =0 at the end of the function.

 virtual void funca() = 0;

The how of creating an abstract class is actually rather simple. It is the why that is problematic. The reason for creating an abstract class is so that you can have a base class that, although it is not appropriate to be used directly, can be inherited. You create an abstract class by making one of its functions a pure virtual function. Remember that a pure virtual function is created by adding =0 to the end of a virtual function’s prototype as you see in the following example.

class abstract { public:       virtual void pure_v_func() = 0; };

Example 12.6

Step 1: Enter this code into your favorite text editor and save it as 12_05.cpp.

#include <iostream> using namespace std; class abstract { public:       virtual void pure_v_func() = 0; }; class derived: public abstract {  public:        void pure_v_func(); }; void derived::pure_v_func() {   cout << "See how this works"; } int main ()  {  derived myclass;  myclass.pure_v_func();    return 0; }

You see how we now have an abstract base class. This is due to the fact that it contains a pure virtual function. This means you now cannot directly instantiate the class. It also means that all the classes methods must be overwritten in the derived class. The purpose of the abstract class is to force the derived class to have a given interface. Other programming languages, such as Java, use an object type called interface to accomplish this. In C++, the abstract class does the same thing. You can think of an abstract class as a template that determines the specific interface that its derived classes will have, while leaving the implementation up to the derived class.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net