C + + member variables are objects of other classes __c++

Source: Internet
Author: User


• In a member function, you can pass the this pointer as a parameter, or you can return the this pointer, or return the object obtained through the this pointer

/* Member variables are objects of other classes * * #include <iostream> using namespace std;
    Date class class Date {int year,month,day; public://constructor Method date () {cout << "date ()" << Endl; }//parametric Date (int _y,int _m,int _d): Year (_y), month (_m), day (_d) {}//void SetValue (int year,int Mont
        H,int Day} {This-> year = year;
        This-> month = month;
        
    This is-> day = day;


}
};
    Employee Class classes employee{string name;//c++ int age;
Date Bir;
    Public:employee (String Name,int age) {cout << "employee's Construction method" << Endl; The last step in creating an object is to invoke the constructor method.
        As long as you open up the memory space, this object has, that is, in the construction method has this pointer this->name = name;
    This->age = age;
    } void Show () {cout << name << "This year" << age << "old" << Endl; //The age of the employee can grow.
        This function returns the age-increasing Employee object employee Growup () {cout << "after a year" << Endl;
        age++; Return *this;//this is the point of the current objectNeedle.
        And I'm going to return the current object. So take the pointer to that value and get it.} employee& GrowUp2 () {cout << "after one year" << Endl;
        age++;
    return *this;




}
}; Main function int main () {//Next I want to create an employee then 1. will open up memory space, 2. Initializes a variable (if the member variable is an object, build it.) When you create this variable, you create an object. That is, creating a Date object calls its construction method. After this variable is created, the employee class's own construction method is invoked. It's the time to create: Object variables must be constructed before the employee class is constructed, 3. Invoke constructor Method employee em ("John", 19); What do you do in the program when you execute to this line of code?
    。
    
    Em.show (); Em.growup ()//someone will ask, your return value of this function is meaningless ah.
    
    
Return void is also the same as em.show (); #pragma now the problem comes.   I want to allow age to grow, do not have to write em.growup ();  Want it to satisfy this syntax em.growup (). Growup (). Growup (). Growup (); Then the Em.growup () is followed by this object, so that you can continue. Growup () ....
    If you return null for this function, then you cannot write Em.growup (). Growup (). Growup (). Growup ();
Em.show ();
    
    #pragma question---why call 4 times is not 24. /******* Note that this is the value of the object passed ********//* EM.GROWUP () called the function when EM itself came in, the function body this->age++; this age is the current object EM member variable, and then the EM back to the object, It was 21 before the return.  But pay attention to return when the Employee growup () {... returns *this};
  equal to Employee = *this growup () {.}  This gets actually a new object instead of EM itself. Create a new object from the current object to return it, i.e. em.growup () .... The type returned is the employee type, but it is not the EM object itself, it is a new object, the new object is added, and it has nothing to do with EM. That is, EM only grew once on the first call/#pragma mark-If you change the return value of the method to a return reference try cout << "age-increasing function return value is a reference type" << end
    L
    Em.growup2 (). GROWUP2 (). GROWUP2 (). GROWUP2 ();
    
    
    Em.show ();
    /****** return value to return reference ******//* employee& = *this growup () {... returns *this}; This is the return of the EM itself, which means that the first em.growup2 () call is finished by the growth of the EM itself.

     And then always call back all the EM itself ...
* * return 0;
 }/* Employee em ("John", 19)//What to do in the program when executing to this line of code. The process of creating the entire object ************ the first step is to open up memory space. How much space does this object occupy?
 Depends on the member variable actually taking up such a large string name;
 int age; Date bir;//and this variable is the day object, how big it is. It also depends on the member variables of the date class.
 
 It has three member variables int year,month,day; The second step initializes the member variable (before the method call is constructed).
 
 If the member variable is an object, build it.
 
 The third step calls the constructor method of this class.
 */


The results of the operation are as follows:

The Date () 
Employee's Construction method
John 19 years old, and
after a year,
John 20 years old
, after a year after a year after one years later, a year after. John is 21 years old. The
age-increasing function return value is a reference type after a year after a year after a year after one years after a year later
John 25 years old. Program
ended With Exit code:0



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.