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

C++Server Side ProgrammingProgramming

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
raja
Published on 03-Apr-2019 15:57:07
Advertisements