The essence of the inheritance of the three basic features of C + + (to be rich for the second generation, or to be clothed with your own hands)

Source: Internet
Author: User

Inheritance, as the literal meaning can be understood, to get the property, but not the ancestors of the dead time to get, in writing this blog before reading the book, quick to read the fall asleep on the right to write it, the feeling of reading no harvest, it is true that the practice is true

Inherited

The way of Inheritance: private (default), Public, Protect (difference: can not be used in subclasses, sub-class objects could access to, general I use the public, the other use, I no longer this, there is a need Baidu on it)

Post-Inheritance results: 1. get the property of the parent class (the variable, whether public or private or protected); 2. access to all (variables and functions) of the parent class----is the way public is inherited, and other inherited methods simply cannot access all of the parent class, but still inherit the

The resulting form of the inheritance: a class an int variable, class b an int variable, b inherits a, builds a object, has four bytes of space, constructs a B object, the first four bytes is an int variable of Class A, and the last four bytes is its own variable

Inheriting me is a bit of a experience when encapsulating multithreaded and extended packaged class extensions:

Don't want to tell the empty content of the story, or how I use it

The nature of inheritance: Reuse of code ---complex variables and functions

I have three cases of inheritance.

1. The parent class I inherit can directly access the member variables and member functions of the parent class

Example: I in the imperfect package multithreading and the version that sells the ticket with the thread, the incoming struct pointer wants the delete, but the destructor cannot pass the parameter, I have built the corresponding type pointer in the parent class, uses this pointer in the subclass to receive the passed in parameter, And then directly deleted the pointers to the parent classes in the destructor of the subclass.

2. I used the inheritance when I implemented the polymorphic virtual function (I'll talk about the virtual function)

3. I built the subclass object, using the constructor of the parent class

Inherit the case, construct the subclass object, see the process in the disassembly


Look at the first picture, I built a Class B object, disassembly of the 011d1413 in the B-class construction, we jumped in the second picture

In the second picture, we see that the top is B () that has not yet entered {}, while the 011d14c6 is jumping to the construct of Class A---and then entering the {} of B.

It is known that after inheriting the subclass object, go into the subclass constructor of the execution body, call the parent class construction, and then execute the subclass constructor body

The essence of the inheritance of the three basic features of C + + (to be rich for the second generation, or to be clothed with your own hands)

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.