2

Possible Duplicate:
C++ Virtual/Pure Virtual Explained

What is the difference between a virtual function and a pure virtual function?

Community
  • 1
  • 1
Sairam Lavu
  • 21
  • 1
  • 1
  • 2

3 Answers3

4

A virtual function can be overridden in a derived class.
A pure virtual function must be overridden in a derived class. Specifically, you can't instantiate a class with a pure virtual function unless at least one derived class overrides that virtual function.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

From Wikipedia:

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class that is not abstract. Classes containing pure virtual methods are termed "abstract;" they cannot be instantiated directly, and a subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class. Pure virtual methods typically have a declaration (signature) and no definition (implementation).

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • So in Java terms does this mean that a virtual function is simply "(instance) method", a pure virtual is "abstract method"? – biziclop Jan 27 '11 at 23:37
  • @biziclop, Yes, but with the requirement that the method be implemented in derived classes. This is the difference between "pure virtual" and "virtual" – Jacob Relkin Jan 27 '11 at 23:38
  • 3
    @biziclop: Yes; all methods in Java are virtual (except those marked `final`, obviously), so when discussing Java, the term is somewhat redundant. It is not redundant, however, in languages like C++ and C#, where methods are *not* virtual by default. – BlueRaja - Danny Pflughoeft Jan 27 '11 at 23:43
0

A pure virtual function is a virtual function that must be redefined by derived classes, thus usually for it there's no implementation in the base class (although it can be provided, to be called by derived classes via the scope-resolution operator; thanks to @Mark Ransom and @Toolbox for pointing it out).

If a class have pure virtual methods it cannot be instantiated, thus any class derived from an abstract class (=a class with pure virtual methods), to be instantiable, must actually define such method.

Example:

#include <iostream>

using std::cout;
using std::endl;

class BaseClass
{
public:

    // A "normal" virtual function which provides a default implementation
    virtual void OnlyVirtual()
    {
        cout<<"BaseClass::OnlyVirtual"<<endl;
    }

    // A "normal" pure virtual function: must be redefined, no default
    // implementation
    virtual void PureVirtual()=0;

    // A pure virtual function that provides a default implementation that can
    // be called only explicitly via scope-resolution operator
    virtual void PureVirtualWImpl()=0;
};

void BaseClass::PureVirtualWImpl()
{
    cout<<"BaseClass::PureVirtualWImpl"<<endl;
}

class Derived0 : public BaseClass
{
public:
    // Define the pure virtual function
    virtual void PureVirtual()
    {
        cout<<"Derived0::PureVirtual"<<endl;
    }

    // notice that, even if there's an implementation of PureVirtualWImpl in
    // BaseClass, since it's marked as pure, Derived0 cannot still be
    // instantiated
};

class Derived1 : public Derived0
{

public:
    // PureVirtual is already defined by the parent class Derived0

    // I must define also PureVirtualWImpl if I want to instantiate this class
    virtual void PureVirtualWImpl()
    {
        cout<<"Derived1::PureVirtualWImpl"<<endl;
    }
};

class Derived2 : public Derived1
{
public:

    // Obviously I can redefine the "normal" virtual function
    virtual void OnlyVirtual()
    {
        cout<<"Derived2::OnlyVirtual"<<endl;
    }

    // Redefine PureVirtual
    virtual void PureVirtual()
    {
        cout<<"Derived2::PureVirtual"<<endl;
    }

    // Just for fun I can redefine PureVirtualWImpl to call the base normally-
    // unaccessible implementation
    virtual void PureVirtualWImpl()
    {
        BaseClass::PureVirtualWImpl();
    }
};

void TestClass(BaseClass & C)
{
    C.OnlyVirtual();
    C.PureVirtual();
    C.PureVirtualWImpl();
}

int main()
{
    // BaseClass b; // <--- compilation error
    // Derived0 d0; // <--- compilation error
    Derived1 d1;
    Derived2 d2;
    TestClass(d1);
    TestClass(d2);
}

Output:

BaseClass::OnlyVirtual
Derived0::PureVirtual
Derived1::PureVirtualWImpl
Derived2::OnlyVirtual
Derived2::PureVirtual
BaseClass::PureVirtualWImpl
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • It is actually possible to provide an implementation of a pure virtual function in the base class. The derived class is forced to override it, but it is still callable by using the scope operator. – Mark Ransom Jan 27 '11 at 23:39
  • That's not necessarily true. You can provide an implementation for a pure virtual function if you want, it's just not common in practice. A scenario where that might be useful is if there are implementation details you want to use in the derived versions, but you still want to force all derived classes to provide an implementation. – Dawson Jan 27 '11 at 23:40
  • @Toolbox @MarkRansom Why in the world would you want to have a base implementation of pure virtual function if that implementation will never be used anyway? – Jacob Relkin Jan 27 '11 at 23:41
  • @Mark Ransom: Fixed, thank you. – Matteo Italia Jan 27 '11 at 23:47