When we create a class, we are always accustomed to using the basic data type of C + + as a constituent part of the class. But in fact, the member data of a class can be either a basic type or a custom type, or it can be an object of a class. This is the combination of classes that describe the case of a class that embeds objects of other classes as members, and the relationships between them are contained in a relationship.
When you create an object of a class, if the class has inline object members, the individual inline objects are created automatically first,
General form of a composite constructor definition:
Class Name:: Class name (formal parameter list): inline object 1 (formal parameter list), Inner front object 2 (formal parameter list) ...
{Initialization of Class}
where "inline object 1 (formal parameter list), the inner Front object 2 (formal parameter list) ..." is called the initialization list, which is initialized with inline objects.
For example
//Create a point classclassPoint { Public: Point (floatXxfloatyy) {x=xx; Y=yy; } intGetX () {returnx;} intGetY () {returny;}Private: floatx, y;};//Create the Rectangle class,classRectangle { Public: //constructor that assigns the passed parameter xp1 to the P1,XP2 assignment to P2Rectangle (Point xp1,point xp2):p 1 (XP1), p2 (XP2) {}voidZC () {floatChang; Chang= ABS ((P1.GETX ()-P2.getx ()) + (P1.gety ()-p2.gety ())) *2; cout<<"The Zhouchang of the rectangle is:"<< Chang <<Endl; } voidArea () {floatAr; Ar= ABS (P1.getx ()-P2.getx ()) *abs (P1.gety ()-p2.gety ()); cout<<"The area of the rectangle is:"<< Ar <<Endl; }Private: Point P1, p2;};
When creating a composite class object, not only the function body of its own constructor will be executed, but the constructor of its nested object will also be called, in the following order:
(1) Call the constructor of the inline object, in the order in which the inline objects appear in the definition of the combined class, note that the order in which the previous objects appear in the constructor's initialization list is independent of the order in which the inline object constructors are called.
(2) The function body that executes this class of constructors.
C + + Classes and objects (2)--a combination of classes