C++ Review Road Thirteen - Virtual Functions and Polymorphism

1. Three Features of C++

Encapsulation: Breaks the concept of C language functions, which can hide the implementation details and make the code modular

Inheritance: enables code reuse and improves programming efficiency

Polymorphism: interface reuse, when writing code in the future, the big framework does not need to be changed, and the code written later can be used.

Second, the specific implementation of polymorphism

To achieve polymorphism, we must first have inheritance. Let's look at an example first.

class Parent
{
public:
	 void print()
	{
		cout << "Print the members of Parent" << endl;
	}
};

class Son : public Parent
{
public:
	 void print()
	{
		cout << "Print Son's members" << endl;
	}
};

We define a Parent class, which has only one public member function. The Son class inherits the Parent class in a public way, so it also inherits the print() function. If we declare an object of class Son and let this object use its member function print() function, the result printed is

print Son's members

This is because there is function rewriting here, and the print() function in the parent class is rewritten in the subclass. So the object of the subclass will call its own member function.

[Note]: Function overriding must appear in the parent class and subclass, and the function declaration must be the same .

Now we define a pointer to the parent class object and let it point to the subclass object, what will happen?

Parent *p = new Son;

The result printed like this is:

Print the members of Parent

But we want to use the pointer of the parent class to point to the object of the child class, and be able to execute the print() function of the child class. At this time, we will use a major feature of C++ - polymorphism.

If I want to implement polymorphism, just add the virtual keyword in front of the return type of the print() function of the parent class, so that we can print the result we want. The print() of the subclass can be added or not added in front of the function. Adding virtual means that this is a virtual function.

Suggestion : When we use polymorphism, we'd better add virtual before the corresponding member function of the subclass, so that people who read the code can clearly know the function of this function.

We can declare the Parent class like this:

class Parent
{
public:
	virtual void print()
	{
		cout << "Print the members of Parent" << endl;
	}
};

Then we use the pointer of the Parent object to point to the object of the Son class, and then execute the print() function to print the data in the subclass. This creates polymorphism, which allows us to use future code without changing the current framework. For example, we inherit a Girl class from the Parent class

class Girl : public Parent
{
public:
	virtual void print()
	{
		cout << "Print the members of girl" << endl;
	}
};

Then we point the Parent's object's pointer to the Girl object, and we can also print "print girl's member", and we don't need to change the big frame.

From the above example, we can see that C++ provides us with a lot of convenience. When using polymorphism, we can perform some different tasks according to different objects, and can be well compatible with future code.

Third, summarize the conditions for the formation of polymorphism

1. There must be inheritance

2. There must be function rewriting, that is, the function prototype of the parent class and the child class are the same.

3. It must point to the subclass through the pointer of the parent class, or use the parent class to refer to the subclass.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325948876&siteId=291194637