C++ - Difference Between A Virtual Function and A Pure Virtual Function - Stack Overflow
C++ - Difference Between A Virtual Function and A Pure Virtual Function - Stack Overflow
1 of 2
http://stackoverflow.com/questions/2652198/difference-between-a-virt...
Possible Duplicate:
C++ Virtual/Pure Virtual Explained
Hi,
Need to know what is the difference between a pure virtual function and a virtual function?
I know "Pure Virtual Function is a Virtual function with no body" but what does this mean and what is actually
done by the line below
virtual void virtualfunctioname() = 0
c++
function
virtual
pure-virtual
4 Answers
A virtual function makes its class a polymorphic base class. Derived classes can override virtual functions.
Virtual functions called through base class pointers/references will be resolved at run-time. That is, the
dynamic type of the object is used instead of its static type:
Derived d;
Base& rb = d;
// if Base::f() is virtual and Derived overrides it, Derived::f() will be called
rb.f();
A pure virtual function is a virtual function whose declaration ends in =0 :
class Base {
// ...
virtual void f() = 0;
// ...
A pure virtual function makes the class it is defined for abstract. Abstract classes cannot be instantiated.
Derived classes need to override/implement all inherited pure virtual functions. If they do not, they too will
become abstract.
In C++, a class can define a pure virtual function that has an implementation. (What that's good for is
debatable.)
edited Sep 28 '12 at 7:26
For a virtual function you need to provide implementation in the base class. However derived class can
override this implementation with its own implementation. Normally , for pure virtual functions implementation
02/04/2013 10:06
c++ - Difference between a virtual function and a pure virtual function ...
2 of 2
http://stackoverflow.com/questions/2652198/difference-between-a-virt...
is not provided. You can make a function pure virtual with =0 at the end of function declaration. Also, a
class containing a pure virtual function is abstract i.e. you can not create a object of this class.
answered Apr 16 '10 at 10:38
Naveen
33.1k 12 71 147
You can actually provide implementations of pure virtual functions in C++. The only difference is all pure
virtual functions must be implemented by derived classes before the class can be instantiated.
answered Apr 16 '10 at 10:39
AshleysBrain
8,910 2 28 62
A pure virtual function is usually not (but can be) implemented in a base class and must be implemented in a
leaf subclass.
You denote that fact by appending the "= 0" to the declaration, like this:
class AbstractBase
{
virtual void PureVirtualFunction() = 0;
}
Then you cannot declare and instantiate a subclass without it implementing the pure virtual function:
class Derived : public AbstractBase
{
virtual void PureVirtualFunction() { }
}
edited Apr 18 '10 at 20:35
1 In C++, a pure virtual function can be implemented. sbi Apr 16 '10 at 10:37
yes, and for the pure virtual destructor, it must be implemented. daramarak Apr 16 '10 at 10:39
@daramarak: pure virtual constructor ? in C++? Naveen Apr 16 '10 at 10:40
@Naveen: stackoverflow.com/questions/2609299/2609404#2609404 sbi Apr 16 '10 at 20:52
@sbi: I read at is constructor instead of destructor. My mistake.. Naveen Apr 17 '10 at 9:51
show 1 more comment
Not the answer you're looking for? Browse other questions tagged c++ function
virtual
02/04/2013 10:06