Ad

Friday, August 10, 2007

Pure Virtual Functions

From the previous article Properties of Virtual Functions, we know that a virtual function may or may not be overridden in the derived lasses. It means, it is not necessary for a derived class to override a virtual function.

But there are times when a base class is not able to define anything meaningful for the virtual function in that case every derived class must provide its own definition of the that function. To force this type of overriding you use the following general form to declare a virtual function:


  virtual ret-type func-name(arg-list)=0;

This type of virtual function is known as Pure Virtual Function.

There are two major differences between a virtual and a pure virtual function, these are below:

  • There CAN’T be a definition of the pure virtual function in the base class.

  • There MUST be a definition of the pure virtual function in the derived class.

By making a virtual function ‘Pure’, it becomes necessary for the derived classes to override it, further since the base class can’t define a pure virtual function, we can’t have objects of that class. These types of incomplete classes (having one or more pure virtual function) are known as Abstract Classes and are used extensively.

The following program illustrates this:


  // Pure Virtual Functions
  #include <iostream.h>

  // base class
  class base
  {
  public:
    // pure virtaul function
    // declaration
    virtual void func() = 0;
    // can't define
  };

  // derived class
  class derived : public base
  {
  public:
    // must define
    void func()
    {
      cout<<"Derived1's func()\n";
    }
  };

  // main
  void main()
  {
    // --CODE: base b
    // won't work because we
    // can't have objects of 
    // absract classes
    derived d1;

    d1.func();
  }

Related Articles:

2 comments:

  1. Anonymous12:31 PM

    Actually, you can provide a definition for a pure virtual function in a base class. However, the difference between a pure virtual function and a simple virtual function is that you will always have to provide a definition for the pure virtual function in the derived class, even if the base class provides a definition, but you don't have to provide a definition for a simple virtual function if the base class provides a definition. Also, note that in order to invoke the definition of the pure virtual function of the base class (from an object of derived class, of course), you will have to qualify the call with the base class name.

    Apart from this, one should always provide an implementation of a simple virtual function in a derived class if that function is declared in the base class interface, even if no client code calls that function on the derived class (and even if a default implementation is provided in the base class). For a non-virtual function, you can get away without providing any implementation, if no client code calls it.

    Here's a code snippet ...

    class B
    {
    public:
    virtual void do_something() = 0;
    virtual void crack_a_joke();
    };

    class D : public B
    {
    public:
    virtual void do_something();
    void crack_a_joke();
    void no_definition();
    };

    void B::do_something()
    {
    std::cout << "In *base* (B) class.\n";
    }

    void B::crack_a_joke()
    {
    std::cout << "Cracking a joke in *base* class.\n";
    }

    void D::do_something()
    {
    std::cout << "In *derived* (D) class.\n";
    }

    void D::crack_a_joke()
    {
    std::cout << "Cracking a joke in *derived* class.\n";
    }


    int main()
    {

    D derived;

    derived.B::do_something();
    derived.crack_a_joke();
    derived.do_something();
    derived.B::crack_a_joke();
    return 0;
    }

    It is important to realize that the ability to provide a definition for a pure virtual function in a base class is not very useful and deviates from the purpose of being a pure virtual in the first place, other than in a very contrived situation. "The purpose of declaring a pure virtual function is to have derived classes inherit a function interface only", whereas, "The purpose of declaring a simple virtual function is to have derived classes inherit a function interface as well as a default implementation."

    ReplyDelete
  2. Anonymous4:26 PM

    Hi Anonymous, that was indeed a very good explanation. thanks

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.