A4-Shallow Copy and Deep Copy
A4-Shallow Copy and Deep Copy
Page 26
Shallow Copy/ Deep Copy, Working With Arrays
3. Concept Map
Although C++ provides you with a basic copy constructor, but still there are occasions when you need to design you
own copy constructor. Given below are some of the reasons why you might want to create a copy constructor.
• You need to copy only some of the data members to a new object.
• Your objects contain pointers.
• Your objects contain dynamic data members.
There may be other numerous reasons why you might want to create a customized copy constructor. Before you
begin you must familiarize yourself with the syntax of a copy constructor. A copy constructor always receives an
object as a parameter and hence we can extract the data from the parameterized object and place the data in the
newly created object. Presented below are the two syntax for copy constructors:
In the above prototypes the object which will be copied is called “other”. By writing the const keyword a copy of an
object can be created without any change to the inherent data members. Although only some of the data members can
be copied.
Page 27
Shallow Copy/ Deep Copy, Working With Arrays
private:
int a;
int b;
public:
example (example & parame) //shallow copy constructor
void showall( )
a=10;
b=20;
};
int main()
example obj1;
example obj2(obj1);
obj1.showall();
obj2.showall();
return 0;
Page 28
Shallow Copy/ Deep Copy, Working With Arrays
In the above code when object obj1 is created the nullary constructor is called and hence values 10 and 20 are allocated
to data members a and b respectively. Now when object obj2 is being created obj1 is passed to the copy constructor
as an argument. While creation of obj2 control is never shifted to the basic constructor because we are attempting to
make a new object by copying.
3.3. Deep Copy Constructor
A deep copy constructor is a constructor that has been designed to handle pointers and dynamic data members.
Although a shallow copy constructor will also copy pointers but it does so in an incorrect way. Hence it is logically
wrong to use a shallow copy constructor for dynamic data members.
The problem with shallow copy constructors:
The problem with shallow copy constructors is that suppose you have a class that has a pointer as a data member and
you want to create a copy of this class object. When you call the shallow copy constructor it will copy the pointer data
member to the new object. You might think this is what we want but in fact it is wrong because copying a pointer
means that you have copied the data and the address to which the pointer is pointing. Hence you have on hand two
objects that are pointing to the same memory location. Always remember that two objects should have their own
distinct identity and distinct memory.
Memory
Copy of
Object 1 Object 1
(a)
Memory
Copy of
Object 1 Object 1
(b)
Figure 1: The effect of copy constructors on a pointer data member (a) using shallow copy (b) Using deep copy
In the code snippet below a deep copy constructor has been provided that creates a copy of a char array. The data
member len is being used to hold the length of the array.
class example
{
private:
char *str;
int len;
public:
example( ); // one normal constructor
example(char *s); // another normal constructor
example(const example &st) //Deep copy constructor
{
len = st.len;
str = new char [len + 1];
strcpy(str, st.str);
}
// other stuff
};
Page 29
Shallow Copy/ Deep Copy, Working With Arrays
When working with copy constructors it is important to remember that the copy constructor function prototype is the
same for both shallow copy and deep copy. Hence the difference lies in the fact that dynamic data members are being
handled or not. To determine if a deep copy or shallow copy constructor is being used you have to study the copy
constructor body.
class example
private:
float a[10]; //array as a data member
public:
example() // normal constructor
// other stuff
};
Given below is a class named example that contains a dynamic floating point array “a”. This array can be initialized
with a constructor or with a simple member function as follows. Since this code works with a dynamic array therefore
the size of the array can be provided at run time. In this particular code since the size is not passed there may be a
possibility that the programmer crosses the array boundary.
class example
private:
float *a; //Dynamic array as a data member
public:
example() // normal constructor
// other stuff
};
Given below is a class named example that contains a dynamic floating point array “a”. This array can be initialized
with a constructor or with a simple member function as follows. Since this code works with a dynamic array
Page 30
Shallow Copy/ Deep Copy, Working With Arrays
therefore the size of the array can be provided at run time. In this particular code since the size is not passed there
may be a possibility that the programmer crosses the array boundary.
Another option which is very popular among programmers is that the size of the array can be stored as a data
member.
class example
private:
float *a; //Dynamic array as a data member
public:
size)
cout<<arr[i].a[j];
};
int main()
Page 31
Shallow Copy/ Deep Copy, Working With Arrays
class english
{
private:
string sentence;
int size;
public:
example()
{
cout<<"Enter your sentence: ";
getline(cin,sentence);
size=9;
size=sentence.length();
}
example (example & tempobj)
{
size = tempobj.size;
sentence=tempobj.sentence;
}
void showall()
{
cout<<"\nSentence: "<<sentence;
cout<<"\nCharacters: "<<size<<"\n";
} };
int main( )
{
english obj1;
english obj2=obj1;
cout<<"Object one contains data";
obj1.showall();
cout<<"Copied object contains data";
obj2.showall();
return 0;
}
Figure 1: The english class demonstrate
Figure 1: The “english” class demonstrating the use of a constructor and a copy constructor.
Page 32
Shallow Copy/ Deep Copy, Working With Arrays
5.5. Compilation
After writing the code, compile your code according to the guidelines mentioned. Remove any errors and warnings
that are present in your code.
5.6. Executing the Program
A sample output after running the program is shown below. Also run the code with other possible inputs.
6. Practice Tasks
Page 33
Shallow Copy/ Deep Copy, Working With Arrays
7. Further Reading
7.1. Books
• Object-Oriented Programming Using C++, Fourth edition, Joyce Farrell
7.2. Slides
• The slides and reading material will be provided by course instructor.
Page 34