Home >Backend Development >C#.Net Tutorial >Summary of C++ Review Key Points No. 9 - Inheritance 2
Method for handling member variables with the same name in inheritance
1. When the member variable of the subclass has the same name as the member variable of the parent class
2. The subclass still inherits the member with the same name from the parent class
3. Distinguish members with the same name in the subclass through the scope discriminator:: (use members of the base class with the same name in the derived class and explicitly use the class name qualifier)
4 , members with the same name are stored in different locations in memory
2. The static keyword in derived classes
Inheritance What will happen if it is combined with the static keyword?
Theoretical knowledge
Ø Static members defined by the base class will be shared by all derived classes
Ø According to the access characteristics of the static members themselves and the inheritance method of the derived class, in There are different access properties in the class hierarchy (comply with the access control of derived classes)
Ø To access static members in a derived class, use the following form to explicitly state:
Class name:: member
Or access the object name through the object. Members
1> Static functions also comply with the three access principles
2> Static is easy to make mistakes (not only must it be initialized, but more importantly, it must explicitly tell the compiler to allocate memory)
3> The constructor defaults to private
Concept of multiple inheritance
Ø The inheritance relationship in which a class has multiple direct base classes is called multiple inheritance
Ø Multiple inheritance declaration syntax
class Derived class name: access control base class name 1, access control base class name 2, …, access control base class name n
{
Data member and member function declaration
};
Ø Class C can inherit members of class A and class B at the same time based on access control, and add
its own members
Multiple inheritance derived class construction and access
Ø The derived class constructor of multiple base classes can use the initializer to call the base class constructor to initialize the data members
Ø Execution The order is similar to the case of single inheritance constructors. The order in which multiple direct base class constructors are executed depends on the order in which each inherited base class is specified when defining the derived class.
ØA derived class object has multiple direct or indirect base class members. There will be no ambiguity when accessing members with different names. If different base classes have members with the same name, they should be identified when accessing derived class objects.
If a derived class is derived from multiple base classes, and these base classes have A common base class, then ambiguity may occur when accessing the name declared in the base class
## Analysis:
Summary:
Ø If a derived class is derived from multiple base classes, and these base classes have a common base class, then the base class When the name declared in the class is accessed,
ambiguity may occur
Ø If there is a common base class on multiple inheritance paths, then somewhere in the inheritance path
Convergence point, this public base class will generate multiple base class sub-objects in the object of the derived class
Ø To make this public base class generate only one sub-object in the derived class, you must Declare this base class as virtual inheritance, making this base class a virtual base class.
Ø Virtual inheritance declaration uses the keyword virtual
Experiment: Note that after adding the virtual keyword, the constructor is called number of times.
3 Inheritance Summary
Ø Inheritance is an important method for realizing software reuse in object-oriented programming. Programmers can define new derived classes based on existing base classes.
Ø A derived class with single inheritance has only one base class. A derived class with multiple inheritance has multiple base classes.
Ø The access of derived classes to base class members is determined by the inheritance method and member properties.
Ø When creating a derived class object, first call the base class constructor to initialize the base class members in the derived class. The order in which destructors are called is the reverse of the order in which constructors are called.
Ø C++ provides a virtual inheritance mechanism to prevent ambiguity in member access in class inheritance relationships.
Ø Multiple inheritance provides powerful functions of software reuse, but also increases the complexity of the program.
The above is the summary of the ninth point of C++ review - Inheritance 2. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!