Basic usage of C++ inheritance and polymorphism

Table of contents

1. Inheritance

1.1 Access level

1.2 Function masking

2. Polymorphism

2.1 Virtual functions


1. Inheritance

There are parent classes and subclasses. This hierarchical relationship is called inheritance, which means that subclasses can inherit many things from the parent class. The concept or nature of inheritance is one of the core ideas of object-oriented programming.

This kind of inheritance needs to define a parent class first. The parent class mainly defines some public member variables and member functions , and then builds a new class by inheriting the parent class. This new class is called a subclass . By inheriting the parent class There are many member variables and member functions in the subclass are automatically inherited to the subclass, so when writing the subclass, a lot of code workload is reduced-only some content unique to the subclass can be written.

We first define a parent class which contains a member variable and member function code as follows:

class base
{
public:
	void base_fun()
	{
		cout << "base" << endl;
	}
	int a = 10;
};

In the definition of a subclass object, the member function of the subclass is included in the subclass to public inherit the parent class (the following will be summarized in detail)

class son:public base//继承方式
{
public:
	void son_fun()
	{
		cout << "son" << endl;
	}
};

When we create a subclass object, we can also call parent class functions and variables. (where access level allows). Note that the program calls the parent class constructor first and then the subclass constructor .

int main()
{
	son son1;
	son1.base_fun();//打印base
	cout << son1.a << endl;;//打印10
	return 0;
}

1.1 Access level

public: can be accessed by any entity
protected: Only member functions of this class or subclasses are allowed to access
private: Only member functions of this class are allowed to access

The following is a summary of access rights and inheritance methods:

Access rights in the parent class Subclasses inherit from parent classes access rights for subclasses
public public public
protected public protected
private public Subclasses do not have access to
public protected protected
protected protected protected
private protected Subclasses do not have access to
public private private
protected private private
private private Subclasses do not have access to

Summarize:

1. If the subclass public inherits the parent class, the access rights of all members of the parent class in the subclass will not change;

2. Protected inheritance turns the public members in the parent class into protected members of the subclass;

3. Private inheritance makes the access rights of all members of the parent class in the subclass private;

4. The private inheritance members in the parent class are not affected by the inheritance method, and the subclasses will never have the right to access;

5. For the parent class, especially the member functions of the parent class, if you do not want outside access, set it to private; if you want your subclasses to be able to access it, set it to protected; if you want to make it public, set it to is public.

1.2 Function masking

In C++ inheritance, the subclass will shadow the function of the same name in the parent class, regardless of the return value and parameters of this function. That is to say, as long as the functions in the parent class and the subclass have the same name, the function in the subclass will cover the function of the same name in the parent class.

When the parent class and the subclass function have the same name, you can add the parent class scope to call the parent class function:

int main()
{
	son son1;
	son1.fun();//打印son
	son1.base::fun();//打印base
	return 0;
}

 

2. Polymorphism

The parent class pointer can new a subclass object, but the subclass pointer cannot new a parent class object. The code is as follows:

	base* base1 = new son;//可以
	son son1 = new base;//报错

Obviously we want to call a function of the same name of the subclass through the new subclass object of the parent class pointer, but in fact it is not possible. In this way, it is outrageous. How can we use the parent class pointer to call the subclass function with the same name? The following introduces the concept of virtual functions.

2.1 Virtual functions

In the parent class, we can set the function as a virtual function by adding the virtual keyword at the beginning of the declaration of this member function. The virtual keyword can also be added before the function of the same name of the subclass (or not) because when the function of the same name of the parent class is set as a virtual function, the function of the same name of the subclass automatically becomes a virtual function.

class base
{
public:
	virtual void fun()
	{
		cout << "base" << endl;
	}
	int a = 10;
};
class son:public base
{
public:
	virtual void fun()
	{
		cout << "son" << endl;
	}
};
int main()
{
	base* base1 = new son;//可以
	base1->fun();//打印son
	delete base1;
	return 0;
}

When using a parent class pointer to call a virtual member function, the dynamically bound fun function is executed. What is dynamic binding? The so-called dynamic is actually when the program is running (running to the line of code that calls the fun function) to know which subclass fun function is called. The virtual function knows which virtual function is called when the program is running. So the virtual function must write its definition part (in case the compiler calls it at any time).

The above is all the content, thank you for watching~

 

Guess you like

Origin blog.csdn.net/m0_74358683/article/details/131715837