# Include <iostream. h>
/*
Copy the initialization constructor test code
The function of copying the initialization constructor is to initialize an object with a known object. In the following three cases, you must use the copy initialization constructor to initialize an object.
Another object.
1. explicitly indicates that when an object is initialized. For example, tpoint P2 (P1 );
2. When an object is passed to a function as a real parameter, for example, P = f (N );
3. When an object is returned as a function value, for example, return R. When the return statement return r is executed, the system uses Object R to initialize an anonymous object. In this case, the system needs to call the copy initialization constructor.
*/
Class tpoint
{
Public:
Tpoint (int x, int y)
{
X = X;
Y = y;
// Cout <"constructor called/N ";
}
Tpoint (tpoint & P );
~ Tpoint ()
{
Cout <"destructor called./N ";
}
Int xcoord ()
{
Return X;
}
Int ycoord ()
{
Return y;
}
PRIVATE:
Int X, Y;
};
Tpoint: tpoint (tpoint & P)
{
X = p. x;
Y = P. Y;
Cout <"Copy initialization constructor called./N ";
}
Tpoint F (tpoint Q );
Void main ()
{
Tpoint M (20, 35 );
Tpoint P (0, 0 );
Tpoint n (m); // 1. It indicates that when an object initializes an object, it calls the copy initialization constructor.
P = f (n); // 2. when an object is passed to a function as a real parameter, the copy initialization constructor is called.
Cout <"P =" <p. xcoord () <"," <p. ycoord () <Endl;
}
Tpoint F (tpoint q)
{
Cout <"OK/N ";
Int X, Y;
X = Q. xcoord () + 10;
Y = Q. ycoord () + 20 ;;
Tpoint R (x, y );
Return R; // 3. When an object is returned as a function, the system uses the object R to initialize an anonymous object. In this case, you need to call the copy initialization constructor.
}
/*
Program running result:
Copy initialization constructor called.
Copy initialization constructor called.
OK
Copy initialization constructor called.
Destructor called.
Destructor called.
Destructor called.
P = 30,55
Destructor called.
Destructor called.
Destructor called.
Press any key to continue
Result description:
1. The copy initialization constructor uses three times.
2. destructor call: When exiting the F () function, the objects defined in this function are released and the system automatically calls the destructor. this is called twice. one is used to release the object Q, and the other is
Is used to release an object like R.
After the main function main () is returned, the value of its anonymous object is assigned to the object P using the value assignment operator, and then the anonymous object is released. Then, an destructor is called.
Finally, when exiting the entire program, three destructor are called to release the objects M, P, and NR defined in the main function.
Therefore, six destructor are called in total.
*/