Home >Backend Development >C#.Net Tutorial >Summary of C++ Review Key Points No. 8 - Inheritance 1

Summary of C++ Review Key Points No. 8 - Inheritance 1

黄舟
黄舟Original
2017-01-16 11:40:191118browse

1 Different inheritance methods will change the access attributes of inherited members

public modified member variables and methods can be used both inside and outside the class.

protected modified member variables and methods are used inside the class and in inherited subclasses, and cannot be used outside the class. (It is for use in the family and for inheritance!)

Private modified member variables and methods can only be used inside the class and cannot be used outside the class

1) In C++ The inheritance method will affect the external access properties of the subclass

Public inheritance: The parent class members maintain the original access level in the subclass

Private inheritance: The parent class members become private members

protected inheritance: public members in the parent class will become protected

protected members in the parent class will still be protected

private members in the parent class will still be private

2) Private members still exist in subclasses, but cannot be accessed. No matter how you inherit a base class, a derived class cannot directly use the private members of the base class.

3) External access attribute table of subclasses in C++

Access level of parent class members

Inheritance method

 public(继承方式)    proteced(继承方式)    private(继承方式)    
public   public   proteced  private    
proteced   proteced   proteced   private    
private   private   private   Private

Three-look principle:

The inheritance method (public, private, protected) in C++ will affect the external access attributes of the subclass

Judge whether a certain sentence can Visited

1) Look at the calling statement. This sentence is written inside and outside the subclass

2) Look at how the subclass inherits from the parent class (public, private, protected)

3) Look at the access level (public, private, protected) in the parent class

Principles for setting the access level of derived class members

Thinking: How Proper use of public, protected and private to declare access levels for members?

1. Members that need to be accessed by the outside world are directly set to public

2. Members that can only be accessed in the current class are set to private

3. They can only be accessed in the current class Members accessed in classes and subclasses are set to protected, and the access rights of protected members are between public and private.

2 Type Compatibility Principle

The type compatibility rule means that wherever a base class object is required, an object of a public derived class can be used instead. Through public inheritance, the derived class obtains all members of the base class except the constructor and destructor. In this way, the public derived class actually has all the functions of the base class. All the problems that the base class can solve can be solved by the public derived class. The substitutions referred to in the type compatibility rules include the following situations:

Subclass objects can be used as parent class objects

Subclass objects can be directly assigned to parent class objects

Subclass objects can directly initialize parent class objects

Parent class pointers can directly point to subclass objects

Parent class references can directly refer to subclass objects

After substitution, derive Class objects can be used as objects of the base class, but only members inherited from the base class can be used.

Type compatibility rules are one of the important foundations of polymorphism.

The first level of meaning:
1-1 The base class pointer (reference) points to the subclass object

Parent *p = Null;
p = &c1;
p->printp();
1-2 指针做函数参数
howToPrint(&p1);
howToprint(&c1);
1-3引用做函数参数
howToprint2(p1);
howToprint2(c1);

The second level of meaning

You can let the parent class object initialize the subclass object

Parent p3 = c1;

Object model in three inheritance

Question: How to initialize the parent class members? What is the relationship between the constructors of the parent class and the subclass?

When constructing the subclass object, you need to call the parent class constructor to initialize its inherited members

In the analysis of the subclass object When constructing, you need to call the parent class destructor to clean up its inherited members

The construction and destructor calling principle in inheritance (from top to bottom during initialization, first Dad has a son again)

1. When the subclass object is created, the constructor of the parent class will be called first

2. After the execution of the parent class constructor is completed, the constructor of the subclass will be executed

3. When the constructor of the parent class has parameters, the call needs to be displayed in the initialization list of the subclass

4. The order of calling the destructor is opposite to that of the constructor

The above is the eighth summary of C++ review points - inheritance one. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn