C + + Review notes-inheritance and derivation

Source: Internet
Author: User
Tags access properties

-Inheritance and derivation

Inheritance (inheritance is to get attributes and behavioral characteristics from ancestors)

1 declaration of a derived class

Class derived classes Name: [Inheritance Method] base class name

{

New data members and member functions for derived classes

};

2 access properties of a base class member in a derived class

3 access rules for derived classes to base class members1) An internal access to a member that is inherited by a new member function in a derived class to the base class (Access 2) The object accesses the member that inherits from the base class by the object of the derived class, Access 3)

1. access rules for private inheritance

2) Access rules for public inheritance

.

3) Protection of inherited access rules

4 constructors and destructors for derived classes

1)

Order of execution of derived class constructors and destructors

When you create a derived class object:

The constructor of the base class---the constructor of the derived class;

When you undo a derived class object:

The destructor of the derived class---the destructor of the base class.

Note : When a base class contains a constructor with parameters, the derived class must define the constructor and prefix ": base class name (parameter table)" To provide a way to pass parameters to the base class constructor.

The generic format of a derived class constructor is

Derived class name (parameter summary table): base class name (parameter table)

{

Initialization statements for new members of derived classes

}

Description: (1) A derived class constructor can be defined outside the class , and only the declaration of the function is written in the body of the class

(2) The Destructors class uses a constructor with no parameters or a constructor with default parameters , which can be omitted when defining a constructor in a derived class: base class ( Parameter Table )";

(3) If the base class of a derived class is also a derived class, each derived class only has to be responsible for the construction of its direct base class , in turn

(4) When executing a destructor for a derived class, the system automatically calls the destructor of the base class to clean up the objects of the base class

Question: When a derived class contains an object member , how should its constructor be constructed?

When an object member is contained in a derived class, the general form of its constructor is:

Derived class name (parameter summary table): base class name (parameter table 0), Object name member 1 (parameter Table 1), ..., object member name n (parameter table N)

{

Initialization statements for new members of derived classes:

}

That

When defining a derived class object, the constructor executes in the following order:

• Call the constructor of the base class;

• Call the constructor of the object member;

• Executes the constructor body of the derived class.

When you undo an object, the call order of the destructor is exactly the same as the order in which the constructor was called.

. Call the destructor of the derived class;

. A destructor that invokes an object member;

. Call the destructor of the base class

5 Other ways to adjust the access properties of a base class member in a derived class

1) member with the same name (the C + + language allows members that are described in a derived class to have the same name as members in the base class)

In order for a derived class to use a member of the same name in a base class, you must precede the member name with the base class name and scope identifier "::",

? can the object of a privately inherited derived class access the public or protected members of the base class

Access statement:

Writes the protected member name or public member name of a base class directly to a segment of the same name in a private derived class definition.

Also named "base class name" in front of the member name

For example:

#include <iostream.h>

Class a{

int x;

Public

A (int x1) {x=x1;}

void print () {cout<< "x=" <<x;};

Class B:private a{

int y;

Public

B (int x1,int y1): A (x1) {y=y1;}

A::p rint; Access statement

};

Main ()

{b b (10,20);

B.print ();

return 0; }

Description: (1) only function names or variable names with no type and parameters can be included in the access declaration

(2) Data members can also use the Access statement

(3) The Access declaration cannot change the original nature of the class member in the base class

Two multiple inheritance and virtual base classes

1 Multiple inheritance (when a derived class has more than one base class, this derived method is called Dalkey derivation or multiple inheritance)

The general form of multiple inheritance declarations is as follows:

Class Derived classes Name: Inheritance Mode 1 base class Name 1, ..., inheritance way N base class name n

{

New data members and member functions for derived classes

};

2 constructors and destructors for multiple inheritance derived classes

The general form of a multi-inheritance constructor definition is as follows:

Derived class name (parameter summary table): base class name 1 (parameter table 1), base class Name 2 (parameter Table 2), ..., base class name N (Parameter table)

{

Initialization statements for new members of derived classes

}

Note:

The order of execution of a multiple-inheritance constructor is the same as that of a single-inheritance constructor:

· The constructor of the base class is executed first;

· Then executes the constructor of the object member;

· Finally, the derived class constructor is executed.

The execution order of destructors is exactly the opposite of the order in which constructors are executed.

3 Virtual base class

1) function

If a derived class derives from more than one base class, and these base classes have a common base class,

When you access members of this common base class in this derived class, you may produce ambiguity

Therefore, a virtual base class is introduced so that members of base classes inheriting from different paths have only one copy in memory

C + + Review notes-inheritance and derivation

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.