C + + Learning Note 44: Inheritance and derivation

Source: Internet
Author: User

Tag: The content code instead of inheriting the order that the derived representation function calls the image class

Combination of classes, inheritance of classes

Class combination (automobile class, Wheel class, at this time can put the wheel class into the car class;)

Class Inheritance (Vehicle class, automobile class, at this time the car class can be derived from the vehicle class;)

Combination: Common description has a.

Inheritance: Commonly described is a.

If both can be used in combination and can be solved by derivation, then the preferred combination method;

The purpose of inheritance and derivation
The purpose of inheritance: to implement design and code reuse

Purpose of derivation: When a new problem arises, the original procedure cannot be solved, and the original program needs to be reformed.

Composition of derived classes

Absorbing the members of the base class

Transforming the members of a base class

Add a new Member

Absorbing the members of the base class

By default, derived classes contain all members except constructors and destructors in all base classes

C++11 specifies that the constructor of a base class can be inherited using a using statement

Transformation of base class members

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

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

Object by derived class: Access only members of public


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: cannot be accessed directly
Access rights:
member functions in derived classes can directly access public and protected members in the base class, but cannot directly access the base class's
Private members;
Any member inherited from a base class cannot be accessed by an object of the derived class;

Protection Inheritance (Protected):
Inherited access Control:

Public and protected members of the base class: both appear in the derived class as protected

Private member of base class: cannot be accessed directly

Access rights:
member functions in derived classes: public and protected members in the base class can be accessed directly, but private members in the base class cannot be accessed directly;

Object by derived class: cannot directly access any member inherited from the base class;

Characteristics and functions of protected members

It is the same nature as the private member for the module that establishes its class object

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

It realizes the data hiding, facilitates the inheritance, and realizes the code reuse.

Class A {

Protected

int x;
};

int main ()

{

A;

a.x = 5;//Error

}

Type conversions

    • A public-derived class object can be used as a base class object, and vice versa;
    • Objects of derived classes can be implicitly converted to objects of the base class
    • An object of a derived class can initialize a reference to a base class
    • A pointer to a derived class can be implicitly converted to a pointer to a base class
    • Using a base class object name, pointers can only use members that inherit from the base class

Note: Do not redefine inherited non-virtual functions

By default:

The construction of a base class is not inherited, and derived classes need to define their own constructors;

C++11 regulations

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

However, only members that inherit from the base class can be initialized, and the new members of the derived class cannot complete initialization;

Grammatical form:

Using B::b;

If you do not inherit the constructor of the base class

New member of derived class: derived class definition constructor completion initialization

Inherited member: The constructor that automatically calls the base class is initialized.

The constructor of the derived class needs to pass parameters to the constructor of the base 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 for this class member):

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

{

Other initialization operations

}

Constructors for derived classes and base classes

When the base class has a default constructor

A derived class constructor can not pass parameters to the constructor of the base class;

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

If you need to execute a constructor with parameters in the base class,

A derived class constructor should provide a reference for the base class constructor;

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 operations

}

Note: The members of this class here refer to the members of the class and, of course, to objects of other classes included in this class;

The execution order is executed in the order of definition;

Copy constructors for derived classes:
If a derived class does not declare a copy constructor, the compiler generates an implicit copy constructor when needed;

The copy constructor of the base class is called first;

Replication is then performed for new members of the derived class;

If a derived class defines a copy constructor

In general, parameters are passed for the copy constructor of the base class, and 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;

The copy constructor form of the base class is a reference to the base class object, and the argument can be a reference to the derived class object;

For example:

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

When accessing a member inherited from a base class,

When the derived class has the same members as the base class:

1. If there is no special qualification, a member of the same name in the derived class is used by the derived class object;

2. If you want to access a member of the same name that is hidden in the base class through a derived class object, you can use the base class name and the scope operation symbol:: To qualify;

The question of ambiguity:
If a function with the same name is inherited from a different base class, but a member with the same name is not defined in the derived class, the derived class object name or reference name. The member name, "derived class pointers, member names" Access has a ambiguity problem.

Workaround: Use the class name to qualify

When inheriting multiple base classes (if two base classes inherit from another same class, it is easy to inherit the same data at this time), it is easy to lead to the same situation of inheriting content, which leads to the redundancy of data and inconsistent data in the course of operation.

Virtual base class

Problem:

When a derived class derives from multiple base classes that have a common base class, then when accessing members in this common base class, redundancy is generated and there is a risk of inconsistency due to redundancy;

Scheme:

Declaring a virtual base class

Virtual description of the base class inheritance method

Example: Class B1:virtual public B

Role:

It is mainly used to solve the two semantic problems that can occur when multiple inheritance is inherited multiple times for the same base class.

Provides a unique base class member for the farthest derived class, rather than repeating multiple copies;

Attention:

When the first level inherits, the common base class is designed as the virtual base class.

Virtual base class and constructors for its derived classes

The class specified when the object is created is called the farthest 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 list. If not listed, the default constructor that calls the virtual base class is called;

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

Objective: To reduce the complexity of the program and improve the reusability of the code.

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

C + + Learning Note 44: Inheritance and derivation

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.