|
|
|
|
|
|
|
FIGURE 13.3
An illustration of a deep copy. |
|
|
|
|
|
|
|
|
When the CATs fall out of scope, their destructors are automatically invoked. The implementation of the CAT destructor is shown on lines 3743. delete is called on both pointers, itsAge and itsWeight, returning the allocated memory to the free store. Also, for safety, the pointers are reassigned to NULL. |
|
|
|
|
|
|
|
|
In this hour you learned how to overload member functions of your classes. You also learned how to supply default values to functions, how to decide when to use default values, and when to overload. |
|
|
|
|
|
|
|
|
Overloading class constructors enables you to create flexible classes that can be created from other objects. The initialization of objects happens at the initialization stage of construction, which is more efficient than assigning values in the body of the constructor. |
|
|
|
|
|
|
|
|
The copy constructor is supplied by the compiler if you don't create your own, but it does a member-wise copy of the class. In classes in which member data includes pointers to the free store, this method must be overridden so that you allocate memory for the target object. |
|
|
|
|
|
|
|
|
Q Why would you ever use default values when you can overload a function? |
|
|
|
|
|
|
|
|
A It is easier to maintain one function than two, and often easier to understand a function with default parameters than to study the bodies of two functions. Furthermore, updating one of the functions and neglecting to update the second is a common source of bugs. |
|
|
|
|
|