C++ Notes - Analysis of the tenth inheritance, detailed and easy to understand

Table of contents

1. The concept and definition of inheritance

1. The concept of inheritance

2. Inheritance definition

2.1 Definition format

 2.2 Inheritance and access qualifiers

2.3 Changes in access methods of inherited base class members 

2. Base class and derived class object assignment conversion

3. Scope in inheritance 

Fourth, the default member function of the derived class 

5. Inheritance and friendship 

6. Inheritance and static members

7. Complex diamond inheritance and diamond virtual inheritance

8. Summary of Inheritance


 



 

1. The concept and definition of inheritance



1. The concept of inheritance


Inheritance (inheritance) mechanism is the most important method for object-oriented programming to make code reusable. It allows programmers to expand and add functions while maintaining the characteristics of the original class, thus generating new classes, called derived classes . Inheritance presents the hierarchical structure of object-oriented programming , reflecting the cognitive process from simple to complex. The reuse we have been exposed to before is function reuse, and inheritance is the reuse of class design levels .

class Person
{
public:
	void Print()
	{
		cout << "name:" << _name << endl;
		cout << "age:" << _age << endl;
	}
protected:
	string _name = "peter"; // 姓名
	
	int _age = 18; // 年龄
};

class Student : public Person
{
protected:
	int _stuid; // 学号
};

class Teacher : public Person
{
protected:
	int _jobid; // 工号
};

int main()
{
	Student s;
	Teacher t;
	s.Print();
	t.Print();
	return 0;
}

2. Inheritance definition


2.1 Definition format

Person is the parent class, also known as the base class. Student is a subclass, also known as a derived class


 2.2 Inheritance and access qualifiers


2.3 Changes in access methods of inherited base class members 

1. The private members of the base class are invisible no matter how they are inherited in the derived class . Invisibility here means that the private members of the base class are still inherited into the derived class object , but the derived class object is grammatically restricted from being able to access it no matter inside or outside the class .


2. The private members of the base class cannot be accessed in the derived class. If the base class member does not want to be accessed directly outside the class, but needs to be accessible in the derived class, it is defined as protected. It can be seen that the protected member qualifier is due to inheritance.


3. The private members of the base class are not visible in the subclasses .

The access method of other members of the base class in the subclass == Min (access qualifier of the member in the base class, inheritance method), public > protected > private.


4. When using the keyword class, the default inheritance method is private, and when using struct, the default inheritance method is public, but it is best to write the inheritance method explicitly.


5. In practice, public inheritance is generally used, and protected/private inheritance is rarely used, and the use of protected/private inheritance is not advocated, because protected/private inherited members can only be used in derived classes. In practice, the extended maintainability is not strong.

 



2. Base class and derived class object assignment conversion



Objects of derived classes can be assigned to objects of base classes/pointers of base classes/references of base classes . There is a figurative term here called slicing or cutting. It means to cut the parent class part of the derived class and assign it to the past .


Base class objects cannot be assigned to derived class objects.

A pointer to a base class can be assigned to a pointer to a derived class by casting. But it must be safe when the pointer of the base class points to the derived class object . Here, if the base class is a polymorphic type, you can use the dynamic_cast of RTTI (Run-Time Type Information) to identify and perform safe conversion.

 

 



3. Scope in inheritance 



1. In the inheritance system, the base class and the derived class have independent scopes. 2. There are members with the same name
in the subclass and the parent class , and the subclass members will block the direct access of the parent class to the members with the same name. This situation is called hiding or redefinition. (In subclass member functions, you can use base class::base class member display access ) 3. It should be noted that if the member function is hidden, only the same function name is required to constitute hiding . 4. Note that in practice, it is best not to define members with the same name in the inheritance system.

 



Fourth, the default member function of the derived class 



"Default" means that if we don't write it, the compiler will automatically generate one for us.

1. The constructor of the derived class must call the constructor of the base class to initialize that part of the members of the base class . If the base class has no default constructor, the call must be explicit during the initializer list phase of the derived class constructor.


2. The copy constructor of the derived class must call the copy constructor of the base class to complete the copy initialization of the base class.


3. The operator= of the derived class must call the operator= of the base class to complete the copy of the base class.


4. The destructor of the derived class will automatically call the destructor of the base class to clean up the members of the base class after being called . Because in this way, the order in which derived class objects are cleaned up first and then base class members is guaranteed.


5. Derived class object initialization calls the base class construction first and then calls the derived class construction.


6. The derived class object destructor cleanup first calls the derived class destructor and then adjusts the base class destructor



5. Inheritance and friendship 



Friend relationships cannot be inherited, that is to say, base class friends cannot access subclass private and protected members



6. Inheritance and static members



If the base class defines a static static member, there is only one such member in the entire inheritance system. No matter how many subclasses are derived, there is only one static member instance.



7. Complex diamond inheritance and diamond virtual inheritance



Single inheritance: When a subclass has only one direct parent class, the inheritance relationship is called single inheritance

Multiple inheritance: When a subclass has two or more direct parent classes, the inheritance relationship is called multiple inheritance

Diamond inheritance: Diamond inheritance is a special case of multiple inheritance

 There will be two copies of the Person member in the Assistant object.

Virtual inheritance can solve the problem of ambiguity and data redundancy of diamond inheritance. As in the inheritance relationship above, the problem can be solved by using virtual inheritance when the Student and Teacher inherit Person. It should be noted that virtual inheritance should not be used in other places.



8. Summary of Inheritance



Inheritance and composition
public inheritance is an is-a relationship. That is to say, every derived class object is a base class object .


Composition is a has-a relationship. Assuming B composes A, there is an A object inside each B object .


Prefer object composition over class inheritance.


Inheritance allows the implementation of derived classes to be defined in terms of the implementation of the base class . This kind of reuse by generating derived classes is often called white-box reuse. The term "white box" is relative to visibility: in inheritance, the internal details of the base class are visible to subclasses.
Inheritance destroys the encapsulation of the base class to a certain extent, and the change of the base class has a great impact on the derived class. The dependency relationship between the derived class and the base class is very strong, and the coupling degree is high.


Object composition is an alternative to class inheritance for reuse. New and more complex functions can be obtained by assembling or combining objects. Object composition requires that the objects being composed have well-defined interfaces. This style of reuse is called black-box reuse because the internal details of the objects are invisible. Objects only appear as "black boxes". There is no strong dependency relationship between composite classes, and the degree of coupling is low . Preferring object composition helps you keep each class encapsulated.


In practice, use as many combinations as possible. The coupling degree of the combination is low, and the code maintainability is good. However, inheritance is also useful. Some relationships are suitable for inheritance, so use inheritance. In addition, to achieve polymorphism, inheritance is also necessary. The relationship between classes can use inheritance, you can use combination, use combination.


 

Guess you like

Origin blog.csdn.net/MuqiuWhite/article/details/129818462