Get the App
SLTechnology News&Howtos  ›  Development  › 

The concept, Diamond inheritance, Virtual inheritance and combination Analysis of C++ data structure inheritance

Shulou Source: shulou.com Published: 2022-06-01 11:28:12 10月03日 Update

This article "C++ data structure inheritance concept and diamond inheritance and virtual inheritance and combination analysis" most people do not understand the knowledge points of the article, so Xiaobian summarized the following content for everyone, detailed content, clear steps, with a certain reference value, I hope you can read this article to gain something, let's take a look at this article "C++ data structure inheritance concept and diamond inheritance and virtual inheritance and combination analysis" article bar.

the concept of inheritance

Inheritance: Inheritance mechanism is the most important means of object-oriented programming to make code reusable. It allows programmers to extend and increase functions on the basis of maintaining the original class characteristics, thus generating new classes, called derived classes. Inheritance presents the hierarchy of object-oriented programming, representing the cognitive process from simple to complex. Previously, the reuse we touched was function reuse, and inheritance was reuse at the class design level.

Definition of Inheritance

Grammar:

Description: Derived classes inherit both member variables and member functions from the base class, but access qualifiers vary depending on how they are inherited.

There are three ways to inherit:

public inheritance

protected inheritance

private inheritance

Access qualifiers:

public access

protected access

private access

Changes in access to inherited base class members:

class member/inheritance method public inheritance protected inheritance private inheritance public member of base class public member of derived class protected member of derived class private member of base class protected member of derived class protected member of derived class private member of base class invisible in derived class

Summary:

The private members of the base class are invisible in derived classes, where invisible refers to private members of the base class or inherited into derived class objects, but syntactically restricts derived class objects from accessing them both inside and outside the class.

Access method of base class member in parent class =min (access qualifier of member in base class, inheritance method), public>protected>private.

Members of the base class that you don't want to have out-of-class access to are generally set to protecd members, not accessible outside the class, but accessible to derived classes.

Assignment conversion between base class and derived class objects

Derived class objects are assigned to objects, pointers, or references of the base class by "slicing" or "cutting." But base class objects cannot be assigned to derived class objects.

Example demonstration:

class Person{public: Person(const char* name = "") :_name(name) {} void Print() { cout

Tags: Members functions objects subclasses combinations pointers methods reuse diamonds data caching code copy concept function design structure two content instance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi vpn Apple OPPO Reno