C ++ learning notes (6) -- how to pass the constructor parameters of multiple inherited class objects

Source: Internet
Author: User
This blog (http://blog.csdn.net/livelylittlefish) posted by the author (three two one, small fish) related research, learning content of the notes made, welcome to the majority of friends correct! Design the base class, derived class, and child-derived class, define the object of the Child-derived class, and observe the passing sequence code of the parameter as follows: /**//*********************************** *************************************
* Multi-level inheritance)
**************************************** ********************************/
# Include <iostream. h>
// Base class cbase
Class cbase
...{
Int;
Public:
Cbase (INT Na)
...{
A = Na;
Cout <"cbase constructor! ";
}
~ Cbase ()... {cout <"cbase deconstructor! ";}
Int geta ()... {return ;}
};

// Cbase derived class cderive
Class cderive: Public cbase
...{
Int B;
Public:
// Pass the parameter na to the base class cbase
Cderive (INT Nb, int Na): cbase (NA)
...{
B = Nb;
Cout <"cderive constructor! ";
}
~ Cderive ()... {cout <"cderive deconstructor! ";}
Int getb ()... {return B ;}
};
// Cderive's derived class csubderive
Class csubderive: Public cderive
...{
Int C;
Public:
// Pass parameters to the direct base class, not to the indirect base class
Csubderive (int nc, int Nb, int Na): cderive (Nb, Na)
...{
C = NC;
Cout <"csubderive constructor! ";
}
~ Csubderive ()... {cout <"csubderive deconstructor! ";}
Int GETC ()... {return C ;}
Void show ()
...{
// Because it is a public inheritance, the Child-derived class can access the public member functions of its parent class and its parent class
Cout <"int the subderive Class Object :";
Cout <"A =" <geta () <Endl;
Cout <"B =" <getb () <Endl;
Cout <"c =" <GETC () <Endl;
}
};

Void main ()
...{
Csubderive OBJ (30, 20, 10 );

Cout <"Call the display function of the object :";
OBJ. Show ();

Cout <"Call the interface function of the object :";
Cout <"A =" <obj. Geta () <Endl;
Cout <"B =" <obj. getb () <Endl;
Cout <"c =" <obj. GETC () <Endl;
} Analysis: 1. the running result is as follows: 2. function execution process: the serial number indicates the execution process of the program. The statements that actually execute are the constructor call statements of 1, 2, and 3, and the execution value assignment statements of 4, 6, and 8. 3. execute Step 1 to return to the main function. The object details are as follows: 4. object member function address: 5. different access methods for member variables inherited from the base class of an object: Note: obj. csubderive is the constructor of the csubderive class of the Child-derived class, while obj. cderive and obj. cbase is a member inherited from the base class. You can access the original base class through obj. cderive. cbase and obj. cbase refers to the same space. The access method here refers to the access method used to view variables in the "watch" tool during tracking (call phase) in the VC environment. You can use the watch tool to view the object's memory space.

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.