Inheritance and derivation of Study Notes

Source: Internet
Author: User

Object-Oriented Programming has four main features: abstraction, encapsulation, inheritance and polymorphism.

 

1.1 concepts of inheritance and Derivation

In C ++, reusability is achieved through the mechanism of inheritance. Inheritance is a only part of C ++.

A new class obtains its existing features from its existing parent class. This phenomenon is called class inheritance. Through inheritance, a new subclass obtains the features of the parent class from the existing parent class.

From another perspective, a new subclass is generated from an existing class (parent class), which is called the derivation of a class.

Class inheritance is the programming technology that uses existing classes to resume special classes.

The derived class inherits all the data members and member functions of the base class, and can add and adjust the members as necessary.

A base class can derive multiple Derived classes. Each derived class can be used as a base class and then generate a new derived class. Therefore, the base class is relative to the derived class.

A derived class can be derived not only from one base class, but also from multiple base classes. A derived class has two or more base classes called multiple inheritance ).

The relationship between the derived class and the base class can be expressed as: the derived class is the concrete type of the base class, while the base class is the abstraction of the derived class.

Inheritance methods include public, private, and protected. This option is optional. If this option is not specified, it is private by default.

The derived class receives members from the base class, and the derived class takes over all the members of the base class (excluding constructors and destructor). That is to say, there is no selection. You cannot select to receive some of the Members, discard another member.

 

Different inheritance methods determine the access attribute of the base class members in the derived class.

1) Public inheritance)

The public and protected members of the base class maintain the original access attribute in the derived class, and their private members are still private to the base class.

2) private inheritance)

The public and protected members of the base class become private members in the derived class, and their private members are still private to the base class.

3) protected inheritance (protected inheritance)

The public and protected members of the base class become protected members in the derived class. The Private Members of the base class are still private.

 

Public inheritance:

When the public inheritance method is used, the public and protected members of the base class still maintain the attributes of the public and protected members in the derived class, the private member of the base class does not become a private member of the derived class in the derived class. It is still a private member of the base class. Only the member functions of the base class can apply it, it cannot be applied by the member functions of the derived class. Therefore, it becomes an inaccessible Member of the derived class.

Private inheritance

The access attribute of the public and protected members of the private base class in the derived class is equivalent to the private member in the derived class, that is, the member functions of the derived class can access them, but cannot access them in the derived class. The Private Members of the private base class become inaccessible members in the derived class. Only the member functions of the base class can apply them. the access attribute of a base class member in the base class may be different from the access attribute in the derived class.

Protection inheritance

If the base class declares private members, no derived class can access them. If you want to access them in the derived class, you should declare them as protected members, if a class declares a protected member, it means that the class may be used as a base class and will access these members in its derived class.

By comparing private Inheritance and Protection inheritance, we can find that in the direct derived class, the above two inheritance methods actually play the same role; no members can be accessed outside the class, in a derived class, you can use a member function to access the public and protected members of the base class.

 

In a derived class, a member has four different access attributes:

1) public, both the derived class and the derived class can be accessed.

2) It is protected and can be accessed within the derived class. It cannot be accessed outside the derived class, but can be accessed from the derived class at the next layer.

3) Private, accessible within the derived class, not outside the derived class

4) inaccessible. It cannot be accessed either inside or outside of the derived class.

 

 

 

Constructor and destructor of a derived class

When declaring a class, you can define no constructor. The system automatically sets a default constructor. When defining class objects, the system automatically calls this default constructor. This constructor is actually an empty function and does not perform any operations. If you need to initialize the data members in the class, you should define the constructor yourself.

The main function of the constructor is to initialize the data members. When designing the constructor of a derived class, we should not only consider the initialization of the newly added data members of the derived class, data member initialization of the base class should also be considered. That is to say, when you want to execute the constructor of the derived class, the database members of the derived class and the data members of the base class will be initialized at the same time, to solve this problem, call the base class constructor when executing the constructor of the derived class.

The general form is

Derived class constructor name (total parameter list): base class constructor name (parameter category)

{New data member initialization statement in the derived class}

Note: When declaring a derived class constructor in a class, the base class constructor name and its parameter list are not included. They are listed only when the function is defined.

In fact, the base class member is initialized in the derived class constructor, that is, the constructor can not only initialize the data member of the constructor using the initialization table, you can also use the initialization table to call the base class constructor of the derived class to initialize the base class data members. You can also implement these two functions in the same constructor definition.

 

When an object is created, the execution order of the constructor is: the constructor of the derived class first calls the constructor of the base class, and then executes the constructor itself (that is, the function body of the constructor of the derived class)

When a derived class object is released, execute the destructor of the derived class first, and then the destructor of the base class.

 

The task of the derived class constructor should include three parts.

Initialize base-class data members, subobject data members, and derived-class data members.

The constructor header of the derived class in the program is as follows:

Student1 (int n, string Nam, int N1, string nam1, int A, string AD): Student (n, NAM), monotor (N1, nam1)

 

To sum up, define the general form of the derived class constructor

Derived class constructor name (total parameter table column): base class constructor name (parameter table column), sub-Object Name (parameter table column)

{New data member initialization statement in the derived class}

The execution order of the derived class constructor is:

Call the base class constructor to initialize the base class data member.

Call the sub-object constructor to initialize the sub-object data member.

Then execute the derived class constructor to initialize the data member of the derived class.

The parameters in the total parameter table column of the derived class constructor should include the parameters in the parameter list of the base class constructor and sub-object. The order of the base class constructor and sub-object can be arbitrary. The header of the derived class constructor can be written

Student (int n, string Nam, int N1, string nam1, int A, stirng AD): Monitor (N1, nam1), student (n, NAM)

 

If no constructor is defined in the base class or a constructor without parameters is defined, you can leave the base class constructor empty when defining the constructor of the derived class, at this time, the derived class constructor does not have a task to pass parameters to the base class constructor. When a derived class constructor is called, the system automatically calls the default constructor of the base class. If no parameter-based constructor is defined in the Declaration of the base class and sub-object type, and you do not need to initialize the data member of the derived class, you do not need to define the constructor of the derived class. At this time, the derived class constructor does not have a task for passing parameters like the base class constructor and sub-object constructor, nor does it have a task for initializing the data members of the derived class. When a derived class object is created, the system automatically calls the default constructor of the derived class provided by the system and executes the default constructor of the derived class, call the default constructor of the base class and the default constructor of the sub-object type.

If a constructor is defined in the Declaration of the base class or sub-object type, the displayed constructor of the derived class is required, write the constructors of the base class or sub-object type and their parameters in the constructor of the derived class.

If no constructor is defined in the base class and a constructor with parameters is defined (the constructor is overloaded), when the constructor of the derived class is defined, you can include base class constructor and its parameters, or do not include base class constructor. When you call a derived class constructor, based on the content of the constructor, you can determine whether to call the base class constructor with or without parameters.

 

 

Destructor of a derived class

When executing the destructor of a derived class, the system automatically calls the destructor of the base class and sub-object to clear the base class and sub-object. The Calling sequence is the opposite to the constructor. First, execute the self-defined destructor of the derived class, clean the newly added members of the derived class, then call the sub-object's destructor to clean up the Sub-objects, finally, call the destructor of the base class to clear the base class.

 

Declare multiple inheritance Methods

If you have declared Class A, Class B, and class C, you can declare a multi-inheritance derived class D:

Class D: Public A, Private B, Protected C

{New member of class D}

The constructor form of the Multi-inheritance derived class is basically the same as that of the single-inheritance class. Only the initial table contains multiple base class Constructor

For example

Derived class constructor name (total parameter list): Base Class 1 Constructor (parameter category), base class 2 Constructor (parameter list), base class 3 Constructor (parameter list)

{New Member initialization in the derived class}

The basic classes are arranged in any order. The constructor of the derived class is also called: First call the constructor of the base class, and then execute the function body of the constructor of the derived class. The sequence of calling the base class constructor is based on the sequence in which the base class is declared.

 

 

Ambiguity of multiple inheritance

 

Virtual base class

Role of the virtual base class: If a derived class has multiple direct base classes, and these direct base classes have a common base class, in the final derived class, multiple members with the same name of the indirect common base class data member will be retained. When referencing these members with the same name, you must add the direct base class name after the Object Name of the derived class, to avoid ambiguity and uniquely identify a member.

In a class, multiple members with the same name are retained to indirectly share the base class. This phenomenon is not expected.

C ++ provides the virtual base class (virtual base class) method, so that only one member is retained when the indirect common base class is inherited.

The method for declaring Class A as a virtual base class is as follows:

Class A // declare the Base Class

{......};

 

Class B: virtual public a // declares that Class B is the public derived class of Class A, and Class A is the virtual base class of Class B.

{......};

 

Class C: virtual public a // Declaration class C is the public derived class of Class A, and a is the virtual base class of class C

{......};

 

Note: The virtual base class is not declared when the base class is declared, but declared when the derived class is declared and the Inheritance Method is specified. Because a base class can be used as a virtual base class when a derived class is generated, while another derived class is not declared as a virtual base class, the general form of the virtual base class is

Class derived class name: base class name in virtual inheritance mode

After such declaration, when the base class is inherited by a derived class through multiple derived paths, the derived class only inherits the base class once.

Note: To ensure that the virtual base class inherits only once in the derived class, it should be declared as a virtual base class in all direct Derived classes of the base class, otherwise, the base class will be inherited multiple times.

 

Initialize the virtual base class

The C ++ compilation system only calls the constructor of the virtual base class by the final derived class, and ignores other derived classes of the virtual base class (such as class B and class C) the call to the constructor of the virtual base class ensures that the data members of the virtual base class are not initialized multiple times.

Note: when defining the constructor of a derived class, it is different from the method used previously. Note: In the final derived class, you should not only initialize its direct base class, but also initialize the virtual base class.

 

When using multi-inheritance, you must be very careful and often encounter ambiguity issues. Many professionals believe that multi-inheritance should not be promoted in programs, but should be used only when it is relatively simple and not prone to ambiguity or when necessary, do not use multi-inheritance for issues that can be solved with a single inheritance. This is also the reason. Some programming languages (such as Java and smalltalk) that support multi-inheritance are not supported.

 

Conversion between base class and derived class

The base class has a value-assignment compatibility relationship between derived class objects. Because the derived class contains members inherited from the base class, you can assign the value of the derived class to the base class object, when a base class object is used, its subclass object can be replaced.

1) A derived class object can be assigned a value to a base class object.

For example

A A1; // defines the base class A Object A1

B B1; // defines the object B1 of Class A's common derived class B

A1 = b1; // use the derived class B object B1 to assign a value to the Base Class Object A1

When assigning values, we discard the members of the derived class. In fact, the so-called value assignment only assigns values to data members, and there is no problem of assigning values to member functions.

Note: After assigning values, you cannot access the B1 Member of the derived class object through object A1, because the B1 member is different from the A1 member.

It should be noted that only subclass objects can be used to assign values to their base class objects, rather than base class objects to objects like them. The reason is obvious, because the base class object does not include a member of the derived class, the member of the derived class cannot be assigned a value.

2) The derived class object can be mentioned to assign values or initialize the base class object to the reference of the base class object.

If you have defined the base class A Object A1, you can define the reference variable A1.

A A1; // defines the base class A Object A1

B B1; // defines the B object B1 of the common derived class

A & R = A1; // defines the reference variable R of the base class A object, and the A1 initializes it.

In this case, the referenced variable r is the alias of A1, and R and A1 share the same storage unit. You can also use a subclass object to initialize the reference variable R and change the last line above:

A & R = b1; // defines the reference variable R of the base class A object, and initializes it with the Class B object B1.

Or keep the above 3rd rows "A & R = A1;" and assign a value to R:

R = b1; // use the derived class B object B1 to assign values to the reference variable R of A1.

 

3) if the function parameter is a reference to a base class object or base class object, the corresponding real parameters can use the subclass object.

4) the address of the derived class object can be assigned to the pointer variable of the base class object, that is, the pointer variable of the base class object can also point to the derived class object.

 

It is legal and safe to direct the pointer variable pointing to the subclass object of the base class object without making any of the above errors, but the application cannot fully meet people's expectations, people sometimes want to use a base class pointer to be a member of the base class and subclass object. In the next chapter, we will solve this problem. The solution is to use virtual functions and polymorphism.

 

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.