Lab exercise for Feb 2
We have studied the situations when
a copy constructor or the assignment operator is
called. We now demonstrate why it is important
to define them properly. Compile and run the programs
indicated below and attempt to answer the questions posed.
(Be prepared to discuss this when your instructor returns.)
-
This program
correctly defines a copy constructor and an assignment
operator. If these are not defined,
the default is a shallow copy.
Try this program (no copy
constructor nor assignment are defined)
and compare its output with the correct program.
Make sure you understand why the output is different.
In particular, for the second program,
- why do v[1] and w[1] have identical values?
- why do v[2] and x[2] have identical values?
- why is z[3] not 30?
-
Now, try understanding the effects of not guarding
against self assignment using
this program .
Comment off
the if clause (line 39) in the assignment function and
then recompile and execute.
Compare before and after outputs.
Now, remove lines 44 and 45 and see what happens when you recompile
and execute.