C + + Fundamentals (11)

Source: Internet
Author: User
Tags class definition

The concept of inheritance for classes

Inheritance is the process of constructing a new class on top of the existing class, whereas derivation refers to the process of creating a new class by adding its own attributes on top of existing classes. The two are different descriptions of the same problem, and inheritance focuses on preserving the properties of the original class, while the derivation focuses on adding new features. The inherited class (that is, the original class) is called the base class, and the derived class is called the derived class. The base class is divided into direct base class and indirect base class.

The purpose of inheritance: to implement design and code reuse

Purpose of derivation: to transform the original class to solve the problem that the base class cannot solve. When the existing function kernel members in the base class are not able to solve the problem, then we need to add data members or function members to solve the problem.

The definition of a derived class when single-inheritance:

Class derived class Name: Inheritance mode base class name

{

Member declaration;

}

The definition of a derived class when multiple inheritance:

Class Derived classes Name: Inheritance Mode 1 base class Name 1, inheritance Method 2 base class name ...

{

Member declaration;

}

by default, derived classes contain all members except constructs and destructors in all base classes , but the C++11 standard also provides the ability to use a using statement to inherit the constructor of a base class. If a new member with the same name in the base class is declared in a derived class, the members in the base class are overwritten.

How to Inherit

The impact of different inheritance methods is mainly embodied in these two aspects, on the one hand, the inheritance method determines the members of the derived class access to the base class members, on the other hand determines the object of the derived class to the base class members access rights. There are three ways of inheriting: public inheritance, private inheritance, and protection inheritance. The characteristics of the three inheritance methods are shown in the following table:

Private Inheritance (private)

    • Inherited access Control
      • Public and protected members of the base class : All appear in the derived class as private ;
      • Private member of base class: not directly accessible .
    • Access rights
      • member functions in derived classes: public and protected members in the base class can be accessed directly, but private members of the base class cannot be accessed directly;
      • Objects derived from a class: You cannot directly access any members that inherit from a base class.

Protect Inheritance (Protected)

    • Inherited access Control
      • Public and protected members of the base class: All appear in the derived class as protected;
      • Private member of base class: not directly accessible .
    • Access rights
      • member functions in derived classes: public and protected members in the base class can be accessed directly, but private members of the base class cannot be accessed directly;
      • Objects derived from a class: You cannot directly access any members that inherit from a base class.

protected characteristics and roles of members

    • It is the same nature as the private member for the module that establishes its class object.
    • For its derived classes, it is the same nature as the public member.
    • It realizes the data hiding, facilitates the inheritance, and realizes the code reuse.
    • If a derived class has more than one base class, that is, multiple inheritance, each base class can be inherited in different ways.

Type conversions

Objects that are derived from a public class can be used as objects of the base class, or vice versa. An object of a derived class can be implicitly converted to a base class object, and a derived class object can initialize a reference to a base class, and a pointer to a derived class can be implicitly converted to a pointer to a base class. Only members that inherit from the base class can be used by the base class object name and pointer. Do not redefine inherited non-virtual functions.

Case code:

#include <iostream>using namespacestd;classBASE1 {//base class Base1 definition Public:    voidDisplay ()Const{cout<<"Base1::d isplay ()"<<Endl; }};classBASE2: PublicBASE1 {//public derived class Base2 definition Public:    voidDisplay ()Const{cout<<"Base2::d isplay ()"<<Endl; }};classDerived: PublicBase2 {//public derived class derived definition Public:    voidDisplay ()Const{cout<<"Derived::d isplay ()"<<Endl; }};voidFun (Base1 *ptr) {//parameter is a pointer to a base class objectPtr->display ();//object pointer, member name}intMain () {//Main functionBase1 base1;//declaring a Base1 class objectBase2 Base2;//declaring a Base2 class objectDerived Derived;//declaring a derived class objectFun (&AMP;BASE1);//Call the fun function with a pointer to the Base1 objectFun (&AMP;BASE2);//Call the fun function with a pointer to the Base2 objectFun (&derived);//Call the fun function with a pointer to the derived object    return 0;}

Run this program we will find: The result of printing has been Base1::d isplay (), this is why? In the following we will learn: if there is a function with the same name in the base class in the derived class, then you need to define this function as a virtual function in the base class, also defined as a virtual function in the derived class, because the non-virtual function is static at compile time, cannot implement dynamic binding, and the virtual function does not compile the concrete function body Rather, it's time to run to determine which function code to execute, so that we can implement a common printing function.

Constructors for derived classes

By default, constructors for base classes are not inherited, and derived classes need to define their own constructors. It is stated in C++11 that the constructor of the base class can be inherited with a using statement, but only members that inherit from the base class are initialized, and if the derived class has new data members, the inherited constructor cannot initialize the new data member. Syntax format: using B::B; If you do not inherit the constructor of the base class, the new members of the derived class need to be initialized by the constructors defined in the derived class, and the members inheriting from the base class will automatically invoke the constructor of the base class for initialization, but where are the parameters of the constructor of the base class passed in? At this point, we need to pass arguments to the constructor of the base class through the constructor of the derived class. The definition syntax for a constructor when single-inheritance:

Derived class Name:: Derived class name (the formal parameter required by the base class, the parameters required by this class member):

base class name (parameter table), this type of member initialization list

{

Other initialization;

};

Multiple-inheritance-time constructors:

Derived class Name:: derived class name (formal parameter list):

base class name 1 (parameter), base class Name 2 (parameter), ... base class name N (parameter)

{

Other initialization;

};

Constructors for derived classes and base classes

When a base class has a default constructor

? A derived class constructor can not pass parameters to a base class constructor.

? When you construct an object of a derived class, the default constructor for the base class is called.

To execute a constructor with parameters in the base class

? Derived class constructors should provide parameters for base class constructors

constructor definition syntax derived from multiple inheritance and with object members

Derived class Name:: derived class name (formal parameter list):

base class name 1 (parameter), base class Name 2 (parameter), ..., base class name N (parameter),

Initializes a list of members of this class (with Object members)

{

Other initialization

};

Execution order of constructors

The first step is to call the base class constructor, in the order in which they are inherited, in the second step, initialize the members in the initialization list, automatically call the constructor of the class to which they belong when the object member initializes, and third, execute the contents of the constructor body in the derived class.

Copy constructors for derived classes

    • Derived class does not declare a copy constructor
      • The compiler automatically generates an implicit copy constructor that calls the copy constructor of the base class and then performs a copy of the new member of the derived class.
    • A derived class defines a copy constructor
      • We need to pass parameters to the copy constructor of the base class. The copy constructor can only accept one parameter, both to initialize the members of the derived class definition and to be passed to the copy constructor of the base class. Because the formal parameters of a base class's copy constructor are references to base class objects, and derived classes can initialize references to base classes, you can use a reference to a derived class object as a parameter when passing arguments to the base class's copy constructor.

Destructors for derived classes

Derived classes if a destructor is required, we need to define the destructor ourselves, which is declared in the same way as the destructor of a class without inheritance, and the destructor does not need to display the call . when the destructor is executed, the destructor for the derived class is executed before the base class destructor is called.

Accessing members inherited from a base class

When a derived class is defined with a member of the same name as the base class, the object of the derived class is using a member of the same name in the derived class without special qualification, and if the hidden member in the base class is accessed through the derived class object, it needs to be qualified by the base class name and the scope operator (::).

The question of ambiguity

When a member of the same name is inherited from a different base class, but a member with the same name is not defined in the derived class, then the name of the derived class object, either the member name or the reference name, or the member name of the derived class pointer, has ambiguity. The workaround is to use the class name qualification.

Virtual base class

When a derived class derives from more than one base class and the base class has a common base class, there are multiple identical members in the derived class, which creates redundancy when accessing members of the derived class, which is not only space-intensive, but is likely to cause inconsistencies. The existence of virtual base class is to solve this problem.

declaration of the virtual base class: class B1:virtual public B. When a virtual base class is declared, there is no two semantic problem that occurs when multiple inheritance takes place on the same base class, and provides a unique base class member for the farthest derived class without producing multiple copies. It is important to note that the common base class is designed as a virtual base class at the first level of inheritance.

Virtual base class and its derived class constructors

    • The class you specify when you create an object is called the furthest derived class.
    • The member of the virtual base class is initialized by the constructor of the furthest derived class by calling the virtual base class's constructor.
    • In the entire inheritance structure, all derived classes that inherit the virtual base class directly or indirectly must list the arguments for the constructor of the virtual base class in the constructor's member initialization table. If not listed, it represents the default constructor that calls the virtual base class.
    • When an object is created, only the constructor of the furthest derived class calls the constructor of the virtual base class, and the other class calls to the virtual base class constructor are ignored.

C + + Fundamentals (11)

Related Article

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.