Q:

What is difference between Virtual function and Pure virtual function in C++?

0

What is difference between Virtual function and Pure virtual function in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

There are some differences between a virtual function and a pure virtual function that I have arranged in a table for easier comparison:

VIRTUAL FUNCTION PURE VIRTUAL FUNCTION
Syntax: virtual int fun(); Syntax:  virtual int fun() = 0;
A virtual function is a member function of the base class which can be redefined by the derived class. A pure virtual function is a member function of the base class whose only declaration is provided in the base class and must be defined in the derived class.
Classes having virtual functions are not abstract. The base class containing pure virtual function becomes abstract.
The definition is given in base class. No definition is given in base class.
The base class having virtual function can be instantiated i.e. its object can be made. The base class having pure virtual function becomes abstract i.e. it cannot be instantiated.
If a derived class does not redefine the virtual function of the base class, then it does not affect compilation. If a derived class does not redefine the virtual function of the base class, then compilation error occurs.
All derived class may or may not redefine the virtual function of base class. All derived classes must redefine the pure virtual function of the base class.

Note: Note that C++11 brought a new use for the delete and default keywords which looks similar to the syntax of pure virtual functions:

my_class(my_class const &) = delete;
my_class& operator=(const my_class&) = default;

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions For Experienced

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Why is a pure virtual function initialized by 0 in... >>
<< What is a pure virtual function in C++?...