Possible Duplicate:
C++ Virtual/Pure Virtual Explained
What is the difference between a virtual function and a pure virtual function?
Possible Duplicate:
C++ Virtual/Pure Virtual Explained
What is the difference between a virtual function and a pure virtual function?
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.
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).
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