C ++ study notes 44: inheritance and derivation, study notes 44

Source: Internet
Author: User

C ++ study notes 44: inheritance and derivation, study notes 44

Class combination, class inheritance

Class combination (automobile, wheel, now you can combine the wheel class into the automobile class ;)

Class inheritance (transportation tools and automobiles. At this time, automobiles can be derived from transportation tools ;)

Combination: Common descriptions of has ..

Inheritance: common description is ..

If the combination can be used and derived, the combination method is preferred;

Purpose of inheritance and Derivation
Purpose of inheritance: To achieve design and code reuse

Purpose of derivation: when a new problem arises, the original program cannot be solved, and the original program needs to be transformed.

 

Composition of a derived class

Absorbing base class members

Modify the member of the base class

Add new members

Absorbing base class members

By default, the derived class contains all the members except the constructor and destructor of all base classes.

C ++ 11 requires that the using statement can be used to inherit the constructors of the base class.

Transform base class members

If the derived class declares a new member with the same name as a base class member, the new member of the derived class hides or overwrites the member with the same name as the outer class.

Public inheritance:
The member functions in the derived class can directly access the public and protected members in the base class, but cannot directly access the private Members in the base class;

Object from a derived class: Only public members can be accessed.


Private ):
Inherited access control:
Public and protected members of the base class: They all appear in the derived class as private;
Private member of the base class: cannot be accessed directly
Access permission:
The member functions in the derived class can directly access the public and protected members in the base class, but cannot directly access
Private member;
Objects of derived classes cannot access any member inherited from the base class;

Protected ):
Inherited access control:

Public and protected members of the base class: They all appear in the derived class as protected.

Private member of the base class: cannot be accessed directly

Access permission:
Member functions in a derived class: You can directly access the public and protected members in the base class, but cannot directly access the private Members in the base class;

Object of a derived class: you cannot directly access any member inherited from the base class;

Features and functions of protected members

For a module that creates its class object, it has the same nature as the private member.

For its derived class, it is of the same nature as the public member.

It not only hides data, but also facilitates inheritance and achieves code reuse.

Class {

Protected:

Int x;
};

Int main ()

{

A;

A. x = 5; // Error

}

 

Type conversion

  • Objects of a public derived class can be used as objects of the base class, but not vice versa;
  • Objects of a derived class can be implicitly converted to objects of the base class.
  • The object of the derived class can initialize the reference of the base class.
  • The pointer of the derived class can be implicitly converted to the pointer of the base class.
  • Only members inherited from the base class can be used through the base class Object Name and pointer.

Note: Do not redefine the inherited non-virtual functions

By default:

The construction of the base class is not inherited. The derived class must define its own constructor;

C ++ 11

You can use the using statement to inherit the base class constructor;

However, only members inherited from the base class can be initialized; newly added members in the derived class cannot be initialized;

Syntax format:

Using B: B;

 

If you do not inherit the constructor of the base class

New member of the derived class: the class definition constructor completes initialization.

Inherited members: automatically calls the base class constructor initialization.

The constructor of the derived class must pass parameters to the constructor of the base class;

 

Definition syntax of the constructor for single inheritance:

Name of the derived class: name of the derived class (the parameters required by the base class and required by the class members ):

Base Class Name (parameter table), class member initialization list

{

// Other initialization operations

}

Constructor of the derived and base classes

When the base class has a default constructor

The derived class constructor does not pass parameters to the base class constructor;

When constructing an object of A derived class, the default constructor of the base class is called;

To execute constructors with parameters in the base class

The derived class constructor should provide reference for the base class constructor;

 

Constructor definition syntax generated when multiple inherited and object members exist

Derived class name: name of the derived class (table of parameters ):

Base Class Name 1 (parameter), base class name 2 (parameter),... base class name n (parameter ),

Initialization list of this class members (including object members)

{

// Other initialization operations

}

Note: The Class Members here refer to the class members. Of course, they can also be the objects of other classes included in the class;

The execution order is executed in the defined order;

 

Copy constructor of the derived class:
If the derived class does not declare a copy constructor, the compiler will generate an implicit copy constructor as needed;

First, call the replication constructor of the base class;

Copy the new member of the derived class;

 

If the derived class defines the copy constructor

Generally, parameters must be transmitted to the base-class copy constructor. The copy constructor can only accept one parameter, which is used to initialize the Members defined in the derived class and pass the parameters to the base-class copy constructor;

In the form of a base class copy constructor, the parameter type is the reference of the base class object, and the real parameter can be the reference of the derived class object;

For example:

C: C (const C & c1): B (c1 ){}

When accessing members inherited from the base class,

When the derived class and the base class have the same members:

1. If there is no special limit, the derived class object uses a member of the same name in the derived class;

2. To access hidden members of the same name in the base class through a derived class object, you can use the base class name and scope operator Symbol: to limit;

 

Ambiguity:
If a function with the same name is inherited from different base classes, but no member with the same name is defined in the derived class, the object name or reference name of the derived class. the access by the member name, "derived class pointer-> member name", has two meanings.

Solution: Use the class name

When multiple base classes are inherited (if two base classes inherit from the same class, the same data is easily inherited), which may easily lead to the same inheritance content, data redundancy and data inconsistency may easily occur during operations;

Virtual base class

Problem:

When a derived class is derived from multiple base classes and these base classes share a common base class, redundancy is generated when you access the members of this common base class, and may cause inconsistency due to redundancy;

Solution:

Declare virtual base class

Use virtual to describe the Inheritance Method of the base class

Example: class B1: virtual public B

Purpose:

It is mainly used to solve the problem of ambiguity arising from multiple inheritance of the same base class during multi-inheritance;

Provides a unique base class member for the farthest derived class, instead of repeatedly generating multiple copies;

Note:

At the first level of inheritance, the common base class should be designed as a virtual base class;

 

Virtual base class and its derived class Constructor

The class specified when an object is created is called the farthest derived class;

The members of the virtual base class are initialized by the constructor of the farthest derived class by calling the constructor of the virtual base class.

In the entire inheritance structure, all derived classes that directly or indirectly inherit the virtual base class must list parameters for the virtual base class constructor In the constructor initialization list. If not listed, the default constructor of the virtual base class is called;

When an object is created, only the constructor of the farthest derived class calls the constructor of the base class. The calls of other classes to the constructor of the virtual base class are ignored.

Objective: To reduce Program Complexity and improve code reusability.

Http://www.xuetangx.com/courses/course-v1:TsinghuaX+00740043_2x_2015_T2+sp/courseware/8d1fd477f469492ba2c1297e6ace6f5d/2a889505dd584a639363f2b4e8726040/

 

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.