C + + virtual functions and inheritance

Posted by zimick on Thu, 16 Dec 2021 23:19:15 +0100

Baidu Encyclopedia's definition of virtual function:

Simply put, those member functions modified by the virtual keyword are virtual functions. The function of virtual function, explained in professional terms, is to realize Polymorphism, which separates the interface from the implementation; explained in visual language, is to realize common methods, but different strategies are adopted due to individual differences.

Wikipedia's definition of virtual function:

In the field of object-oriented programming, C + + There is the concept of virtual function (English: virtual function) or virtual method (English: virtual method) in languages such as Object Pascal. This function or method can be inherited and overridden by subclasses, usually implemented by dynamic dispatch. This concept is in object-oriented programming In short, virtual functions can give the definition of the objective function, but the specific direction of the objective function may not be determined at compile time.
Click transfer

inherit

Data can be inherited and occupy a part of memory; Function inheritance inherits the calling right of the function (a subclass can call the function of the parent class).
There are three types of member functions:

Non virtual function, virtual function and pure virtual function

A C + + Example

The application framework has been written in advance, and the Serialize() method is not implemented in the base class (the Serialize() method is implemented when the application is implemented). ps: although it is not implemented in the base class, the base class uses this method

Looking at the main function, we can call the method OnFileOPen() of the parent class through the subclass
When it's all written, it's cdocument:: onfileopen (& MyDoc); The pointer of the subclass is passed as a parameter, so that the implementation of the subclass can be found when the Serialize() function is executed in the OnFileOpen() function of the parent class.


code:

#include<iostream>
using namespace std;

class CDocument
{
public:
	void OnFileOpen()
	{
		//Simulate opening a file
		cout << "dialog.." << endl;
		cout << "check file status..." << endl;
		cout << "open file..." << endl;
		Serialize();
		cout << "close file..." << endl;
		cout << "update all views..." << endl;
	}
	virtual void Serialize() {};

};

class CMyDoc :public CDocument
{
public:
	virtual void Serialize()
	{
		//Only the application itself knows how to read its own files
		cout << "CMyDoc::Serialize()" << endl;
	}
};

int main()
{
	CMyDoc myDoc;
	myDoc.OnFileOpen();
}

Operation results:

Java

In the Java language, all methods are "virtual functions" by default Only methods marked with the keyword final are non - virtual functions The following is an example of a virtual method in Java:

import java.util.*;

public class Animal {
   public void eat() { System.out.println("I eat like a generic Animal."); }
 
   public static void main(String[] args) {
      List<Animal> animals = new LinkedList<Animal>();

      animals.add(new Animal());
      animals.add(new Wolf());
      animals.add(new Fish());
      animals.add(new OtherAnimal());

      for (Animal currentAnimal : animals) {
         currentAnimal.eat();
      }
   }
}
 
public class Wolf extends Animal {
   public void eat() { System.out.println("I eat like a wolf!"); }
}
 
public class Fish extends Animal {
   public void eat() { System.out.println("I eat like a fish!"); }
}
 
public class OtherAnimal extends Animal {}

Output:
I eat like a generic Animal.
I eat like a wolf!
I eat like a fish!
I eat like a generic Animal.

This phenomenon is called upward transformation in Java
For example, Animal fish = new Fish(); This fish can only call methods inherited or overridden from the parent class (its own unique methods are lost).
For the above eat() method, the subclass will call its own if it implements it, and the subclass will call the one given by the parent class if it does not implement it.

----------------------
Summary: defining a function as a virtual function does not mean that the function is not implemented.
It is defined as a virtual function to allow the function of a subclass to be called with a pointer to the base class.
Defining a function as a pure virtual function means that the function has not been implemented.
A pure virtual function is defined to implement an interface and play the role of a specification. Programmers who inherit this class must implement this function.
Postpone implementation
The call of a class function is not determined at compile time, but at run time. When writing code, it is not sure whether the function of the base class or the function of which derived class is called, so it is called "virtual" function.
Virtual functions can only achieve polymorphism by means of pointers or references.

Topics: Java C++