C++ programming foundation [3] (relationship between classes)

1. Inheritance relationship

In C++, the most general class is called a base class, while the more specific class is called a derived class, the more general class is also called a super class, and the more specific class is also called a subclass Derived classes inherit from the base
class All members (with some exceptions) in , which can also add new members

1. Inheritance relationship

The most commonly used inheritance is public inheritance.
The public members in the base class are called public members in the derived class.
To use the same name for the functions in the base class and the derived class, you need to overload or override the member function
constructor, destructor Functions and assignment operators are not inherited and must be redefined
In delegation, derived member functions delegate some of their responsibilities to the base class using the class resolution operator (::)
In invocation, derived class constructors during initialization Call the constructor of the base class, which does not require class resolution operators
Private data members are more encapsulated, and protected data members can simplify the coding of derived classes ()
use the modifier final to prevent
inheritance An object must always be replaceable by an object of a subclass without changing any properties of the superclass
Private inheritance does not define type inheritance, it defines implementation inheritance

2. Relationship

1. Aggregation relationship

Aggregation is a one-to-many relationship from Aggregator to Aggregated
In Aggregation, the lifecycle of the Aggregator is independent of that of the Aggregator
(separate instantiation)

2. Combination relationship

The lifecycle of the contained depends on the lifecycle of the containment
Contained are created inside the containing object, they do not have an independent lifecycle
The lifecycle of the contained depends on the lifecycle of the containment
(only one class instance change)

3. Dependencies

Class A depends on class B and needs to use class B

2. Polymorphism

1. Polymorphism

1. Conditions for polymorphism

The three conditions of polymorphism are pointers or references, exchangeable objects, and virtual functions.
C++ recommends that we always define an explicit destructor for the base class of polymorphism and call it a virtual function. Use virtual analysis Constructors prevent memory leaks that can occur in polymorphism

2. Binding

1. Static binding

Static binding occurs when a function contains multiple definitions, but the compiler knows which version of the definition to use when compiling the program

2. Dynamic binding

Dynamic binding is required when the object is not known during compilation

3. Runtime type information

1. Use the typeid operator

Use typeid(*).name to return the length and name of the class name

2. Use the dynamic_cast operator

Adds a lot of overhead to the system and is not recommended

2. Abstract class

Concrete classes can instantiate and create objects of different types
An abstract class is a class that has at least one pure virtual function
An abstract class cannot be instantiated because there are no pure virtual member functions in the abstract class
A special case of an abstract class when defining an interface, where All member functions are pure virtual

3. Multiple inheritance

1. Virtual base inheritance

One of the solutions to the problem of repeated shared data members in multiple inheritance is to use virtual base inheritance. The intermediate class can inherit from the common base with the virtual keyword, and set the shared data members as protected data members.

2. Mix in class

Mixin classes are another solution, mixin classes are never instantiated, but mixin classes can add data members to other classes

Guess you like

Origin blog.csdn.net/qq_37249793/article/details/130965315