CMPT 225 Lab 2: Shallow copy, deep copy, and copy constructor

CMPT 225 Lab 2: Shallow copy, deep copy, and copy constructor


In this lab we will see what a copy constructor is and how it is necessary when we work with linked lists.

Copy constructor is the function that runs when you try copying an instance of a class into another class. For example in the following code, the copy constructor copies the information of p1 into p2.

#include "patient.h"

int main(){
	patient p1("Golnar", 3);
	patient p2 = p1;
}	

Even if we don't define the copy constructor, the above-mentioned piece of code would run because there is a default copy constructor that just copies the value of all member data from the source to the destination object. Sometimes (like in the case of patient), that is all we need. Sometimes, that is when we are dealing with pointers, this could be problematic. We will see such a case in this lab, and implement a copy constructor ourselves.

Lab steps

  1. Download this zip file and unzip it. Look at the header file of intSLinkedList. There are two function signatures that are commented out. One of them is the destructor (the one that starts with ~) and the other one is the copy constructor. Just like a normal constructor, it doesn't have a return type, but it takes a const reference of the class it is in, as input. Leave both function signatures commented for now.

  2. Open the test_intSLinkedList.cpp and try to figure out what it's doing. In the first portion of the main function, we are adding 5, 12, and 6 to the int_sll list and printing the list. In the second portion, we are copying int_sll into a new list int_sll2 and printing the new list. We expect them to be the same so far. In the third (and last) part of the code, we are adding 100 to int_sll list only and then printing both lists. The expected behavior is for int_sll to contain 100 and for int_sll2 not to contain it.

  3. Compile and run test_intSLinkedList to see if the behavior matches our expectations. Remember test_intSLinkedList.cpp depends on intSLinkedList class. So, we have to compile intSLinkedList as a class first and then compile tes_intSLinkedList. If you're having problems with compiling the code, check out this quick tutorial on g++.

  4. As you can see, int_sll2 contains 100 too. That doesn't match the expected behavior. This is because the default copy constructor copies the value of head, which is only an address, into the destination list's head. This means both heads (source and destination lists') point to the same list. So, there is only one list, and two pointers to the beginning of it. This is called shallow copying (copying only the address) and it is not what we want. We need to perform a deep copy, that is to actually copy the whole list. We need to create the nodes one by one, copy the data in the new node and go to the next node, until we hit the end of the source list. Now uncomment the copy constructor's signature in the header file, and implement it in the cpp file. Check out this file to get a clear idea on how you should implement the deep copy. Notice the difference between the first copy (when destination is empty) and the rest of copies. Also, don't forget to account for the case when source list is empty.

  5. Compile and run test_intSLinkedList again and see if the behavior is as expected (it should be).

  6. Now uncomment the destructor in header and implementation files and compile and run test_intSLinkedList again. Everything should still be good.

  7. Now, comment the copy constructor in header and implementation files but leave the destructor uncommented. Compile and run test_intSLinkedList again. It should abort. Can you explain why?

  8. Comment the destructor in both .h and .cpp files again. Now you have no copy constructor and no destructor; so, you're back to square one. In tes_intSLinkedList.cpp, try changing the int_sll.addBack(100) to int_sll.addFront(100) and redo step 3. Is the behavior as you expected? Can you explain what happened?

  9. Uncomment both destructor and copy constructor in intSLinkedList.cpp file and upload it to CourSys.

  10. If you would like an explanation for what you saw in steps 7 and 8, come to class!

What to submit?

  1. Your version of intSLinkedList.cpp with the constructor and uncommented destructor.

Where to submit?

In CourSys under CMPT 225 Lab2 activity.