# Include <iostream>
# Include <vector>
Using namespace STD;
Struct exmp1 {
// Default constructor
Exmp1 ()
{
Cout <"exmp1 ()" <Endl;
}
// Copy the constructor
Exmp1 (const exmp1 &)
{
Cout <"exmp1 (const exmp1 &)" <Endl;
}
// Value assignment operator
Exmp1 & operator = (const exmp1 & RHE)
{
Cout <"operator = (const exmp1 &)" <Endl;
Return * this;
}
// Destructor
~ Exmp1 ()
{
Cout <"~ Exmp1 () "<Endl;
}
};
Void func1 (exmp1 OBJ) // The parameter is an exmp1 object.
{
}
Void func2 (exmp1 & OBJ) // reference of an exmp1 object
{
}
Exmp1 func3 ()
{
Exmp1 OBJ;
Return OBJ; // return the exmp1 object
}
Int main ()
{
Exmp1 eobj; // call the default constructor to create the exmp1 object eobj;
Func1 (eobj); // call the copy constructor.
// Create an exmp1 object as a copy of an exmp1 object
// Call the destructor to cancel the exmp1 object after the function is executed.
Func2 (eobj); // reference an exmp1 object. You do not need to call the copy constructor to pass the real parameter.
Eobj = func3 (); // call the default constructor to create a local exmp1 object
// Call the copy constructor to create an exmp1 object as the return value copy when the function returns
// Call the destructor to cancel the partial exmp1 object.
// Call the value assignment operator
// After the value assignment operation is completed, call the destructor to cancel the exmp1 object as the copy of the returned value.
Exmp1 * P = new exmp1; // call the default constructor to dynamically create an eamp1 object
Vector <exmp1> evec (3); // call the default constructor to create a temporary object value (eamp1 ).
// Call the copy constructor three times to copy the temporary value eamp1 object to each element of the evec of the vector container.
// Then call the destructor to cancel the temporary value eamp1 object
Delete P; // call the destructor to cancel the dynamically created eamp1 object
Return 0; // when the lifecycle of evec and eobj ends, the Destructor is automatically called to undo it.
// Call the Destructor three times to cancel evec
}
Running result:
Exmp1 ()
Exmp1 (const exmp1 &)
~ Exmp1 ()
Exmp1 ()
Exmp1 (const exmp1 &)
~ Exmp1 ()
Operator = (const exmp1 &)
~ Exmp1 ()
Exmp1 ()
Exmp1 ()
Exmp1 (const exmp1 &)
Exmp1 (const exmp1 &)
Exmp1 (const exmp1 &)
~ Exmp1 ()
~ Exmp1 ()
~ Exmp1 ()
~ Exmp1 ()
~ Exmp1 ()
~ Exmp1 ()