Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I know about the definition of pure virtual function but what is purpose of pure virtual function
Posted
Updated 6-Sep-11 2:58am
Comments
Reiss 6-Sep-11 8:59am    
Please don't use all caps - it is considered shouting.

A pure virtual function makes the class an abstract class, which itself cannot be instantiated. This means that to use the class, you have to inherit from it, and you MUST override all pure virtual functions defined in the abstract class.
 
Share this answer
 
v2
Comments
CPallini 6-Sep-11 9:04am    
My 5.
Uday P.Singh 6-Sep-11 9:07am    
correct my 5!
Espen Harlinn 6-Sep-11 18:32pm    
Agree :)
Philippe Mori 6-Sep-11 20:19pm    
A pure virtual function (and an abstract class) has a similarity with functions inside an interface because they need to be implemented by derived classes. The main difference is the intended usage (implementation of the base class vs public interface to a class).
Further to John's answer this code snippet should show you what is going on.

C#
namespace Dummy
{
    public abstract class PureVirtualFunction
    {
        public abstract void Method();
    }

    public abstract class VirtualFunction
    {
        public virtual void Method()
        {
            // do something
        }
    }

    public class PureVirtualFunction_Inherited : PureVirtualFunction
    {
        public override void Method()
        {
            // must provide own implentation
        }
    }

    public class VirtualFunction_Inherited_Base : VirtualFunction
    {
        // uses base method implementation
    }

    public class VirtualFunction_Inherited_Overrides : VirtualFunction
    {
        public override void Method()
        {
            // my implementation
        }
    }
}
 
Share this answer
 
Comments
BillWoodruff 6-Sep-11 9:17am    
+5 a very good set of didactic examples !
Uday P.Singh 6-Sep-11 9:26am    
my 5!
Espen Harlinn 6-Sep-11 18:32pm    
Nice workout - 5'ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900