Here is a link to what the Fraction class would look like with these functions added, although it should be noted that they are not needed in the Fraction class, because the automatic versions are sufficient.The Fraction member data does not involve pointers or dynamic allocation -- it only consists of static data, so the shallow copy is enough.This is sufficient for many cases, but not for ALL cases.
These functions include: The automatic versions of the constructor and destructor don't do anything, but they will be there if you do not build them.
(The constructor you get is the "default constructor" -- no parameters -- and this is automatically built if you don't define any constructor besides the cop constructor).
Since the copy constructor is constructing a brand new object, it won't be the same as the parameter right. the current object is a brand new object and its data members have just gotten memory, and have no valid values in them.
They are garbage values, meaning that array Ptr may or may not be a valid address.
This will especially pose problems if, when the copy goes out of scope, it cleans up the dynamic data along with it.
When there is a pointer (inside an object) that points to dynamic data, the shallow copy is not sufficient, because it does not copy the dynamic data, only the pointer. Here is what we might write for a copy constructor definition in the Directory class (from the phonebook database example): Like the copy constructor, the assignment operator has to make a copy of an object. If a deep copy is desired for assignments on a user-defined type (e.g.
To return the object, we need to be able to refer to an object from inside the object itself.
From inside any member function, an object has access to its own address through a pointer called ) Like the copy constructor, the original object needs to be passed in, so there will be one parameter (of the same type as the object itself).
An assignment operator sets an existing object's state to that of another existing object.
In situations with dynamic allocation, this may mean that old dynamic space must be cleaned up first before the copy is made.