C ++ getting started learning notes inheritance

xiaoxiao2021-03-06  111

1. Concept and importance of inheritance

Inheritance: It is a form of software reuse that organizes related classes, and is a common data and operational behavior between Heng.

The most attractive feature: New categories can be inherited from existing class libraries. Advocate new categories with existing classes to achieve software reuse

Features that can add base classes and replacement and improvement from the base class inheritance

Single weight inherits the formation of a tree hierarchy, which constitutes a hierarchical relationship by the base class and derived class, and the inherited level is arbitrary in the limits of the system.

The inheritance mechanism defines the father and child relationship

2. Base class

* The parent class defines all subclasses to communicate with the external interface and private implementation content, and the parent class is called the base class.

* Member function:

Private members of the base class can only be accessed by the member functions and friend of the base class.

Protected members of the base class can only be accessed by the base class and derived members.

* Inheritance of the way

Public public inheritance uses the most, public key, protected, private

Protected: Public, Protection Protection, Private: Public, Protection

3. Derived class

* New class inherits the data member and member functions of the base class without having to revoke data members and member functions, this new class is derived (Derived) class

* Detective class never directly access the private member of the base

* Redefile functions

Functions that do not need inheritance in the derived class and the base class feature to be extended can redefine member functions, but when derived class, use the same name function of the base class, use the functional domain operator EMPLOYEE :: Print ()

* Detective class constructor and destructor

Since the derived class inherits a member of the base class, when the exemplary object of the derived class is established, the constructor of the base class must be called to initialize the base class member in the partial class object. The implicitly invoke the base class constructor, or in the derived class's constructor to provide an initialization value (list of member initialized value lists) in the constructor of the derived class (list).

Constructor call sequence, first execute the constructor of the base class -> Member object constructor of the derived class -> derived class constructor

The sequence of destructor calls is just the opposite.

Remember: Detective Class does not inherit the construction function, destructive function and assignment operator of the base class, but the constructor of the derived class and the assignment operator can call the constructor and assignment operation of the base class. symbol

4. Base class VS derived class

Understand points: You can see the base class and derived class as a type of int

* Object

The object of derived class can be treated as a base class, that is, the defined derived class object TIME T can access member functions of the base class and protected data, and derived class objects are also reasonable. Conversely, the objects of the base class cannot be automatically used to become the object of derived class, because the derived class has some objects of the expansion base class.

* Pointer

The pointer to the derived class can be implicitly converted to the base class pointer. That is, the derived pointer can directly assign the base class pointer, but it will not

However, it is possible to convert the base class pointer to a derived type pointer with an explicit type to convert it to a derived class object (because it does not exist without reference to this object before conversion). Detective class object) such as Char * C; (int *) C;

* Both relationships

Direct base class pointer reference base class object: Time * t, c; * t = & c; * t Return to the reference T -> (member)

Directly use the derived class pointer to the derived class

Use a base class pointer to reference a derived class object, but only reference base classes (useful in polymorphism)

Time * T, Time2 C; * T = & C; * T Returns reference to the object of the party, T -> (base class member)

Use the library pointer to reference the base class object, absolutely no. It must be forced to convert to the base class pointer

Time T, Time2 * C; * C = & T, * C and C -> (member) errors

Time T, Time2 * C; * c = (time *) C C-> (member) correct

The pointer operation is to use the arrow member to select an operator to access members.

No matter how to assign a value, the reference to the object must have this object or member function - Skyala

5. Multiple inheritance

* A class can be derived from multiple base classes, which is called multiple inherits. It is a graphic hierarchy (with a directionless). Note: Powerful function, but it is easy to cause two-purpose role in the use of one-purpose

* Usage: A type A, is type B, but also type C

* After the colon (:) plus public base class list class t: public d, public c {}

转载请注明原文地址:https://www.9cbs.com/read-98905.html

New Post(0)