Explain in detail the inheritance of C ++ classes

Source: Internet
Author: User
Tags multiple inheritance in c

A private or protected derived class is not a subclass, because a non-public derived class cannot do anything that the base class can do, that is, it can be used in public, but within the class.

I. Introduction

In C ++, a class is a logical unit that provides encapsulation. Each object of a class contains a data set that describes its own State, this data set is also processed by receiving specific messages. If the program designer can crop the class by adding, modifying, or replacing part of the content of the specified class, it can adapt to different applications, in this way, the value of data encapsulation is greatly enhanced, and the inheritance discussed next can fully implement this operation.

Ii. Basic concepts related to inheritance

Inheritance is a process. through inheritance, an object can obtain the attributes (including functions) of another object and add its own characteristics to it. As an important mechanism of the C ++ language, the inheritance method can automatically provide an operation and data structure from another class for one class, in this way, programmers can quickly create a new class on the basis of a general class without having to design each class from scratch.

When a class is inherited by other classes, the inherited class is called a base class (not a chicken ribs ^_^), or a parent class.

A class that inherits other class attributes is called a derived class and also a subclass.

Generally, the inherited process originates from the definition of a base class, which defines the public attributes of all its derived classes. Essentially, the base class has the public attributes in the same class set. The derived class inherits these attributes and adds its own unique attributes. The essence of inheriting from any existing class is to build a new derived class.

Iii. Single-inheritance, multi-inheritance and inheritance chain

The inheritance derived from a base class is called single inheritance. In other words, a derived class has only one direct base class. The common format of a single inheritance declaration statement is:

Class derived class name: Access Control keyword base class name
{
Data member and member function declaration
};

In contrast, inheritance derived from multiple base classes is called multi-inheritance or multi-inheritance. That is, a derived class has multiple direct base classes. In some object-oriented languages (such as Java), multi-inheritance between classes is not supported, but only single-re-inheritance is supported. That is, a class can only have one direct parent class at most, therefore, you need to use interfaces and other mechanisms to implement similar functions. The syntax support for multiple inheritance in C ++ makes the problem much simpler. The common syntax of multiple inheritance declaration statements is:

Class derived class name: Access Control keyword base class name 1, access control keyword base class name 2 ,...
{
Data member and member function declaration
};

In addition to multi-inheritance, a derived class inherits multiple base classes, and another method is to use the derived class as the base class for another class to inherit again, resulting in a multi-level inheritance relationship. For example, Class A is a Class B, Class B is a derived class C, Class A is the direct base class of Class B, and Class B is the direct base class of class C, class A is the indirect base class of class C. The hierarchy of classes is also called the inheritance chain. In the above example, when a class C object is created, the constructor of Class A is called first, and then the constructor of Class B is called, finally, it is the constructor of class C. The Calling sequence of the Destructor is the opposite. When a derived class inherits a hierarchical class, each derived class on the inheritance chain must pass the required variables to its base class.

Iv. Public and private Derivation

In an inherited declaration statement, the access control keyword is used to indicate the extent to which the members and member functions declared in the base class definition can be accessed by the derived class. The access control keyword can be public, private, or protected. If the access control keyword is public, the derived class is inherited from the base class public, also known as public. If the access control keyword is private, the derived class inherits from the base class private, also known as private. The differences between public inheritance and private inheritance are listed below.

Through the above table, we can summarize the features of the two types of derivation as follows:

Base class member Base class private member Public Member of the base class
Derivation Method Private Public Private Public
Derived class member Invisible Invisible Visible Visible
External Functions Invisible Invisible Invisible Visible

(1) No matter which method of derivation, the Private Members in the base class are invisible in the derived class. That is to say, the private member in the base class cannot be accessed by any member in the external function or derived class.

(2) the difference between "public" and "private" is that the access attribute of the Public Member in the base class in the derived class:
When public is derived, the Public Member in the base class is equivalent to the Public Member in the derived class.
When private is derived, the Public Member in the base class is equivalent to the private member in the derived class.

Therefore, private derivation ensures that the methods in the base class can only be used indirectly by the methods of the objects in the derived class, rather than externally. Public derivation allows both the derived class object and the external class to directly use the methods in the base class, unless these methods have been redefined.

5. Protection of Members and protection Derivation

If you want to allow a base class member to be accessed only by a member with a derived kinship, but not by a non-kinship object Member, either public or private derivation is not possible. Because the Private Members of the base class members are inaccessible to other class (including the derived class) members, when the public members of the base class are derived from the public, they can not only be accessed by the object members of the derived class, it can also be accessed by external functions. In private, although the public members in the base class allow access by members of the derived class object and do not allow external access, when the next level is assigned, since all the members of the base class have been privatized, other Class Members cannot access the base class. Only objects with derivative kinship can be accessed by using the protected member with another access attribute in the base class.

A protected member is a kinship member. It is a public member for a derived object and can be accessed. It is hidden like a private member for external kinship.

In addition to private and public, C ++ supports protected. The following table lists the changes in the access attributes of the three members with different access attributes in the derived classes.

Derivation Method Public Member of the base class Protected Member of the base class Private member of the base class Overview of access attribute changes caused by Derivative Methods
Private Change to private member Change to private member Invisible Non-Private Members in the base class become private members in the derived class.
Protected Derivation Changed to a protected member. Change to private member Invisible The access attribute of non-private members in the base class in the derived class is lowered by one level.
Public-derived Still a Public Member Still a protected member Invisible The access attribute of non-private members in the base class remains unchanged in the derived class.

It should be noted that the private member of the base class is invisible in the derived class no matter what type of derivation.

Vi. youyuan class and youyuan Function

(1) User Functions

Generally, private members of a class can only be accessed by members of the class. External functions can only be member functions of the class, and then Private Members of the member functions of the class. However, if an external function (perhaps a member of another class) is declared with friend in a class definition, this external function can access any private member of the class with exceptions. The external function declared with friend is called the friend function of this class.

When a friend function is a member function of another class, pay attention to the following points:

A: When a friend function is a member function of a class, in addition to declaring it in the class definition, it should also declare its friend relationship in another class, statement format:

Name of the class where the friend function belongs: function name (parameter list );

B: When you reference a private member of an object of this class, you do not need to reference the parameter of the object of this class, however, when you reference a private member in an object of A youyuan class, you must include a reference parameter of the youyuan class object.

C: When a member function of a class is used as a friend function of another class, it must be defined first, instead of just declaring it.

Using a friend function to directly access a private member of an object saves the overhead required to call a member function of the class. At the same time, as an interface of the class, you can use external functions to supplement the functions of a designed class by adding a declaration statement, or build a bridge between different types of objects. However, it also destroys object encapsulation and information hiding, so be careful when using it.

(2) youyuan

You can also declare a class, not just a function, as a friend class of another class. In this case, you only need to declare it first, instead of defining it first.

It should be noted that the relationship between friends and friends is unidirectional and valid only between two classes. Even if Class X is a friend of class y, whether class Y is a friend of Class X depends on whether there is a corresponding declaration in class X. That is, the relationship between friends and friends is not interchangeable. If Class X is a friend of class Y and class Y is a friend of Class Z, it does not necessarily mean that Class X is a friend of Class Z, that is, the relationship between friends and Yuan is not transmitted.

It is useful to make one class A friend class of another class when one class is to work together with another class. At this time, every member function in the friend class becomes the friend function of the other party.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.