Base class, derived class, memory allocation ?. XML

Source: Internet
Author: User
Pre {Line-Height: 1; color: # 1e1e1e; Background-color: # d2d2d2; font-size: 16px ;}. sysfunc {color: #627cf6; font-style: italic; font-weight: bold ;}. selffuc {color: #800080 ;}. bool {color: # d2576f ;}. condition {color: #000080; font-weight: bold ;}. key {color: #000080 ;}. vaR {color: #800000; font-style: italic ;}. digit {color: # ff00ff; font-weight: bold ;}. includepre {color: # 1e1e1e ;}. operator? {Color: #008000; font-weight: bold ;}

The derived class inherits the base class.

?????? During memory allocation, it is located in different memory addresses of the base class object. Based on the member variable type of the base class, a space of the same type is opened, but note that the space of the derived object after the development is opened, instead of copying the value of the base class member, it just opens up the space of the member type. When it is not initialized, the number of members in it is uncertain.

??????? The member variables defined by the derived class are placed under the inherited Class A member. If the variable name defined by the derived class is the same as that defined by the base class, the variable overwrites the inherited base class variable with the same name, note that overwriting is not deleted, too? That is, the member variables inherited from the base class in the derived class still exist, and the value does not change. If you want to use this member variable to inherit from the base class, add ::?,? When accessing a member function, you can directly use base: I, if the member variables inherited from the base class are accessible to the object (Public type), use? A. Base: access by I

?

Base?base;Sub_class??sub;base.i;base.j

However, when an object is used to access the member variables of the derived class that inherit from the base class (assuming the public type)

Use:

sub.Base::isub.Base::j

Directly use sub. I ,? Access the custom I of the subclass

Such? The following procedure

???? Class? Base {???? Public :?????????? Int? I ;?????????? Int? J ;????};???? Class? Sub: public? Base {????? Public :????????????? Int? I ;????????????? Int? J ;???????? Void? P (){?????????????????? Cout <base: I <Endl ;?????????????????? Cout <base :: j <Endl ;?????????????????}????????????};??? ??? Base? B ;???? Sub? S ;???? B. I = 1 ;???? B. J = 2 ;???? S. I = 3 ;???? S. J = 4 ;???? S. P ();????????????? //??? Output Two uncertain numbers? 4425598 ??? 4421322 ???? Cout <B. I <Endl ;?? //??? 1 ???? Cout <B. j <Endl ;??? //??? 2 ???? Cout <S. I <Endl ;??? //??? 3 ???? Cout <S. j <Endl ;??? //??? 4 ???? Cout <S. Base: I <Endl ;???? Cout <S. Base: j <Endl;

The output result is as follows:

4425598

4421322

1

2

3

4

4425598

4421322

Press?any?key?to?continue?.?.?.

The comparison between the values of the two base class variables inherited by the derived class object and the member variables of the base class object is sufficient to verify the above conclusion:

The member of the base class inherited by the subclass only opens a member variable of this type in another memory space. Its value is not the value of the base class, the compiler is only responsible for setting this part of the space type to the same type as the base class.

Used in this article? Calligraphy and painting novel software? Released, the content has nothing to do with the software, and the painting and calligraphy novel software? More comfortable reading, more comfortable writing, and easier Publishing.

Base class, derived class, memory allocation ?. XML

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.