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

Jan 16, 2017 am 11:40 AM

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
Copy after login

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);
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the role of char in C strings What is the role of char in C strings Apr 03, 2025 pm 03:15 PM

In C, the char type is used in strings: 1. Store a single character; 2. Use an array to represent a string and end with a null terminator; 3. Operate through a string operation function; 4. Read or output a string from the keyboard.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

Four ways to implement multithreading in C language Four ways to implement multithreading in C language Apr 03, 2025 pm 03:00 PM

Multithreading in the language can greatly improve program efficiency. There are four main ways to implement multithreading in C language: Create independent processes: Create multiple independently running processes, each process has its own memory space. Pseudo-multithreading: Create multiple execution streams in a process that share the same memory space and execute alternately. Multi-threaded library: Use multi-threaded libraries such as pthreads to create and manage threads, providing rich thread operation functions. Coroutine: A lightweight multi-threaded implementation that divides tasks into small subtasks and executes them in turn.

How to calculate c-subscript 3 subscript 5 c-subscript 3 subscript 5 algorithm tutorial How to calculate c-subscript 3 subscript 5 c-subscript 3 subscript 5 algorithm tutorial Apr 03, 2025 pm 10:33 PM

The calculation of C35 is essentially combinatorial mathematics, representing the number of combinations selected from 3 of 5 elements. The calculation formula is C53 = 5! / (3! * 2!), which can be directly calculated by loops to improve efficiency and avoid overflow. In addition, understanding the nature of combinations and mastering efficient calculation methods is crucial to solving many problems in the fields of probability statistics, cryptography, algorithm design, etc.

How to apply snake nomenclature in C language? How to apply snake nomenclature in C language? Apr 03, 2025 pm 01:03 PM

In C language, snake nomenclature is a coding style convention, which uses underscores to connect multiple words to form variable names or function names to enhance readability. Although it won't affect compilation and operation, lengthy naming, IDE support issues, and historical baggage need to be considered.

distinct function usage distance function c usage tutorial distinct function usage distance function c usage tutorial Apr 03, 2025 pm 10:27 PM

std::unique removes adjacent duplicate elements in the container and moves them to the end, returning an iterator pointing to the first duplicate element. std::distance calculates the distance between two iterators, that is, the number of elements they point to. These two functions are useful for optimizing code and improving efficiency, but there are also some pitfalls to be paid attention to, such as: std::unique only deals with adjacent duplicate elements. std::distance is less efficient when dealing with non-random access iterators. By mastering these features and best practices, you can fully utilize the power of these two functions.

C# vs. C  : History, Evolution, and Future Prospects C# vs. C : History, Evolution, and Future Prospects Apr 19, 2025 am 12:07 AM

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

Usage of releasesemaphore in C Usage of releasesemaphore in C Apr 04, 2025 am 07:54 AM

The release_semaphore function in C is used to release the obtained semaphore so that other threads or processes can access shared resources. It increases the semaphore count by 1, allowing the blocking thread to continue execution.

See all articles