Why is a C++ pure virtual function initialized by 0?


It’s just a syntax, nothing more than that for saying that “the function is pure virtual”.

A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in declaration.

Here is an example of pure virtual function in C++ program

Example Code

 Live Demo

#include<iostream>
using namespace std;
class B {
   public: virtual void s() = 0; // Pure Virtual Function
};

class D:public B {
   public: void s() {
      cout << " Virtual Function in Derived class\n";
   }
};

int main() {
   B *b;
   D dobj;
   b = &dobj;
   b->s();
}

Output

Virtual Function in Derived class

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements