Third week notes C + + Boolan

Source: Internet
Author: User

This week, we mainly learned about the relationship between classes and classes in C + +, mainly:

1) Inheritance (inheritance);

2) compound (composition);

3) Commission (delegation);

First, compound:

There are deque in the class queue, where the class queue is a container, and it has a deque, and the relationship is compound, which means has-a. The two come together.

Look at this relationship from a memory point of view:

Constructors and destructors under a composite relationship:

The constructor is from the inside Out:

The Container constructor first calls the component default constructor before executing itself. The process is as follows: note: The procedure to invoke the default constructor for component will be done automatically by the compiler, but you will need to add it yourself if you want to call component's non-default constructor.

Container::container (...): Component ()  

The destruction is from the outside and inside:

The destructor of Container first executes itself before calling component's destructor, as follows:

Container::~container (...) {... ~component ()};

Ii. delegation (connected by a pointer)

The class String has a pointer to stringred, which is the delegate. Class string can call stringred at any time and delegate the task to stringred. The two are out of sync, create on the left and wait until you need the right to create the right.

Iii. inheritance (is a)

There are three ways of inheriting in C + +: Private inheritance, protected inheritance, and public inheritance, the most important of which is public inheritance. Its basic syntax is as shown.

Construction and destruction under the inheritance relationship:

Constructed from inside to outer:

The constructor of the subclass first calls the parent class's default constructor before executing itself.

The destruction is from the outside and inside:

The destructor of the subclass first executes itself before the destructor of the parent class is called.

The use of inheritance with virtual functions:

1) Non-virtual function: The parent class already has a definition, and the subclass cannot redefine it.

2) virtual function: The parent class already has a default definition, but the subclass can still be redefined if needed.

3) There is no definition in the parent class, and the subclass must be redefined when used.

An example of the use of inheritance with virtual functions:

Iv. extensive application of entrustment + inheritance

1) Observer mode

2) Combination mode

Typical application scenario: Windows folder with file system, folder with files

3) Prototype mode

Scenario: Prototype mode is a quick and easy way to create new objects through the interface of an already existing object.

Third week notes C + + Boolan

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: 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.