The 15th part of the C ++ notes series -- the construction of the derived class, base class, and sub-object and the call relationship of the destructor

Source: Internet
Author: User

Example
Example 1
Note: If a base class generates two Derived classes at the same time, that is, the two Derived classes inherit from the same base class, the system will copy each resume, each derived class uses its own copy of the base class (for example, the base class has its own static variables ).
# Include

Class Person
{
Public:
Person () {cout <"Construction of person." < ~ Person () {cout <"Destruction of person." < };
Class Student: public person
{
Public:
Student () {cout <"Construction of student." < ~ Student () {cout <"Destruction of student." < };
Class Teacher: public Person
{
Public:
Teacher () {cout <"Construction of teacher." < ~ Teacher () {cout <"Destruction of teacher." < };

Int main ()
{
Student s;
Teacher t;
}
Program output:
Construction of person.
Construction of student.
Construction of person.
Construction of teacher.
Destruction of teacher.
Destruction of person.
Destruction of student.
Destruction of person.
Conclusion: if a program contains multiple objects, each object must be created in order. When creating a derived class object, the sequence of calling constructor is as follows: base Class Construction --> sub-Object Construction --> derived class construction. The Calling sequence of the Destructor is strictly opposite to that of the constructor. In the constructor of a derived class, when the constructor of the base class defaults to a parameter, the base class constructor can call the base class constructor by default in the derived class constructor.

Example 2

# Include
Class Data
{
Public:
Data (int x) {Data: x = x; cout <"Data construction." < ~ Data () {cout <"Data destruction." < Private:
Int x;
};
Class Base
{
Public:
Base (int x): d1 (x) {cout <"Base construction." < ~ Base () {cout <"Base destruction." < Private:
Data d1;
};
Class Device: public Base
{
Public:
Device (int x): Base (x), d2 (x) // share Parameters
{Cout <"Device construction." < ~ Device () {cout <"Device destruction." < Private:
Data d2;
};

Int main ()
{
Device obj (5 );
}
Program output:
Data construction.
Base construction.
Data construction.
Device construction.
Device destruction.
Data destruction.
Base destruction.
Data destruction.
Structural sequence analysis: calls the Base class construction, and the Base contains the sub-object d1. Therefore, you must call the d1 constructor before calling the Base constructor; after the Base constructor is called, The d2 constructor is called. The final step is to call the constructor of the derived class.

Example 3

Class Base
{
Public:
Base (int xx = 0): x (xx) // note that there is no semicolon, which is equivalent to a common constructor type.
{Cout <"Base construction." < ~ Base () {cout <"Base destrcution." < Void print () {cout < Int Getx () {return x ;}
Private:
Int x;
};
Class Device: public Base
{
Public:
Device (itn xx = 0, int yy = 0): Base (xx), y (yy), z (xx + yy)
{Cout <"Device construction." < ~ Device () {cout <"Device destruction." < Void print ()
{
Base: print ();
Cout < }
Private:
Int y;
Base z;
};
Int main ()
{
Device obj1 (1), obj2 (4, 6 );
Obj1.print ();
Obj2.print ();
}
Program output:
Base construction.
Base construction.
Device construction.
Base construction.
Base construction.
Device construction.
2, 0, 2
4,6, 10
Analysis structure ......

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.