C + + Classes and objects (2)--a combination of classes

Source: Internet
Author: User
Tags abs gety

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

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.