Talk about C + + Session 5: Member objects (Member objects) and enclosing classes (enclosing class)

Source: Internet
Author: User

member object: the member variable of one class is an object of another class
enclosing class: A class that contains member objects


give me a chestnut:

Class Tyre {private:int radius; int width; Public:tyre (int r, int w): Radius (r), Width (w) {}};class Engine {};



Class Car {//This category is the so-called closed class//which includes the member object engine and Tyreprivate:int price; Tyre tyre; Engine engine;public:car (int p, int tr, int tw);}; Car::car (int p, int tr, int w):p rice (p), Tyre (tr, W) {};int main () {Car car (20000,17,225); return 0;}


Specification:
1. When a member object occurs, the constructor of the class contains the initialization of the member. The default constructor of a member object is used if the member initialization list of the constructor does not initialize the member object.
2. When creating an object of a class, its constructor should be called first. However, if the class has member objects, the constructor for the class to which the member object belongs is executed before the constructor of the current class is executed when all the member objects have executed their own class constructors.


For example:
Class Tyre {Public:tyre () {cout << "Tyre contructor" << Endl;} ~tyre () {cout << "Tyre destructor" << Endl;}; Class Engine {Public:engine () {cout << "Engine contructor" << Endl;} ~engine () {cout << "Engine destructor" << Endl;}}; 7class Ccar {private:engine Engine; Tyre Tyre;public:car () {cout << "Car contructor" << Endl;} ~car () {cout << "Car destructor" << Endl;}}; int main () {Car Car;return 0;}


its output is:
Engine contructor
Tyre contructor
Car contructor
Car destructor
Tyre destructor
Engine destructor


according to the specification, the sequence of calls is not the same as the compiler order, so the output is different.


"Tips for reference"
#include <iostream>using namespace Std;class date{public:date () {cout<< "This is Date" <<endl;}// The default constructor for date date (int A) {cout<< "the value=" <<A<<endl;}; Class time{Public:time () {cout<< "This is Time" <<endl;//time's default constructor time (int A):d 2 (a)//time constructor. The parameter of the member object D1 is initialized with the member initialization list, and the parameter of the member object D2 is initialized to a{cout<< "hello!" <<endl;} Private:date D1, D2; In time, declare two data members D1 and D2, two members are two objects of the date class, that is, D1 and D2 are member objects};void main () {Time T1, T2 (6);//Comments 1cout<< "the end" << Endl;}


Note 1:
when the main program runs to time T1, T2 checks to see if there is a member object in the date class, checks for the D1, D2, and discovers the member objects D1 and D2, executes the constructors in the corresponding date class for D1 and D2, and then executes the constructor in time.
the specific steps are:
(i) when creating a T1 object (no parameters), follow these steps:
1, executes the constructor of the D1 class Date, because D1 is not initialized in time, so the default constructor is called when overloaded, and the output is date;
2, executes the constructor of the D2 class Date, because D1 is not initialized in time, so the default constructor is called when overloaded, and the output is date;
3. Execute the constructor of class time of T1, because D1 is uninitialized in time, so the default constructor is called when overloaded, and the output of this was is;
(b) When creating a T2 object (with parameters), follow these steps:
4, executes the constructor of the D1 class Date, because D1 is not initialized in time, so the default constructor is called when overloaded, and the output is the is date.
5, executes the constructor of the D2 class date, according to time time (int a):d 2 (a), found that D2 has been initialized to D2 (a), here is D2 (6), thus need to call the date class's constructor date (int a), output the Value=.
6. Execute the constructor of T2 class time, and the overloaded function of T2 should be time (int A) and output Hello.


"Reference Tips original address:Http://blog.csDn.net/rhzwan123/article/details/2105205 "

Talk about C + + Session 5: Member objects (Member objects) and enclosing classes (enclosing class)

Related Article

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.