A pure virtual function is a virtual function declared by assigning 0 to it. Pure virtual function is used to create abstract class in C++. A class is an abstract class if there is at least a single pure virtual function. A pure virtual function must override in the derived class, otherwise the derived class will also become abstract class.
* Pure virtual function can has an implementation in base class outside class definition.
* Abstract methods may not be declared final. It can be private or protected, but cannot used by base pointer or reference.
* Implementing an abstract method in a subclass follows the same rules for overriding a method. The name and signature must be the same, and the visibility of the method in the subclass must be at least as accessible as the method in the parent class.
```
#include<iostream>
using namespace std;
class Base {
protected:
int i;
public:
Base():i(1) {}
virtual void show()=0;
};
void Base::show() { cout<<i<<endl; } // pure virtual function can still have its implementation
class Derived:public Base {
public:
void show() { cout<<2*i<<endl; }
};
int main() {
Derived d;
Base &b=d;
b.show(); // 2
b.Base::show(); // 1
return 0;
}
```