What Is Pure Virtual Function?

A pure virtual function is a special kind of virtual function. In many cases, a virtual function cannot be given a meaningful implementation in the base class. Instead, it is declared as a pure virtual function, and its implementation is left to the derived base class Class to do it. This is what a pure virtual function does.

Pure virtual function

pure
1.For convenience

Pure virtual function polymorphism

Refers to different implementation actions when the same object receives different messages or different objects receive the same message. C ++ supports two types of polymorphism: compile-time polymorphism and runtime polymorphism.
a. Compile-time polymorphism: implemented by overloading functions and operator overloading.
b Run-time polymorphism: implemented through virtual functions and inheritance.

Virtual function

A virtual function is a member function that is declared as virtual in the base class and redefined in the derived class, which enables dynamic overloading of member functions

Pure virtual function abstract class

A class containing pure virtual functions is called an abstract class. Because abstract classes contain pure virtual functions that are not defined, you cannot define objects of abstract classes.
Program example:
Base class:
  class A {
 public:
 A ();
 virtual ~ A ();
 void f1 ();
 virtual void f2 ();
 virtual void f3 () = 0;
 };

Subclass:
  class B: public A {
 public:
 B ();
 virtual ~ B ();
 void f1 ();
 virtual void f2 ();
 virtual void f3 ();
 };
Main function:
  int main (int argc, char * argv []) {
 A * m_j = new B ();
 m_j-> f1 ();
 m_j-> f2 ();
 m_j-> f3 ();
 delete m_j;
 return 0;
 }
f1 () is a concealment.For the concealment of functions, you can refer to other entries.
Calling m_j-> f1 (); will call f1 () in class A, which will be fixed when we write the code.
That is, according to it is defined by class A, so the function of this class is called.
f2 () is a rewrite (overwrite).
Calling m_j-> f2 (); will call this function corresponding to the object stored in m_j. This is due to new B
Object. (Call f2 () of derived class)
f3 () is the same as f2 (), except that there is no need to write a function implementation in the base class.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?