Professional Documents
Culture Documents
Shallow Copy and Deep
Shallow Copy and Deep
(LAB-05)
Shallow Copy / Deep Copy
Working with Arrays
Table of Contents
1. Introduction 43
4. Concept Map 43
4.1 Creating a customized copy constructor 43
4.2 Shallow Copy Constructor 44
4.3 Deep Copy Constructor 45
4.4 Working with Arrays 46
7. Practice Tasks 49
7.1 Practice Task 1 49
7.2 Practice Task 2 49
7.3 Outcomes 50
7.4 Testing 50
9. Evaluation Criteria 50
Lectures: 8, 9, 10
4. 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 data members. Although only some of the data members can be copied.
private:
int a;
int b;
public:
example (example & parame) //shallow copy constructor
a=parame.a;
b=parame.b;
void showall( )
cout<<"\na="<<a;
cout<<"\nb="<<b;
a=10;
b=20;
};
int main()
example obj1;
example obj2(obj1);
obj1.showall();
obj2.showall();
return 0;
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.
(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
};
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]; //Static 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
a=new float[10]; //The size can be passed from the main to the 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.
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:
example(int size)
a=new float[size]; //The size is passed from the main to the constructor
void showall (example arr[ ], int size) //Size is passed to restrict from crossing array boundary
cout<<arr[i].a[j];
};
int main()
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.
6.3.2 Compilation
After writing the code, compile your code according to the guidelines mentioned. Remove any errors and warnings
that are present in your code.
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to finish the
tasks in the required time. When you finish them, put these tasks in the following folder:
\\dataserver\assignments$\OOP\Lab05
main and assign values to the data member of the object. Then create another object named “two” that is a copy of the
“one”. Create a shallow copy constructor and then demonstrate that both objects share a common memory i.e.
modifying one object in fact modifies the other. Create a display function that will show the values of the object.
7.3 Outcomes
After completing this lab, students will be able to conveniently use a copy constructor in both deep copy and shallow
copy mode. Further the students will have the ability to comfortably use arrays in their various forms both inside and
outside the class.
7.4 Testing
Test Cases for Practice Task-1
Sample Inputs Sample Outputs
Colour = Hot Red After selective copying only the permanent attributes contain
Owner = Ali Raza values.
Year of manufacture = 2013 Colour = Hot Red
Seats = 2 Owner =
Cubic Capacity = 5700 Year of manufacture =
Engine number = 123456 Seats = 2
Frame number = 987654 Cubic Capacity = 5700
Engine number =
Frame number =
9. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is assigned marks
which will be evaluated by the instructor in the lab depending on the accomplishment of the assigned tasks.
10.1 Books
- Object-Oriented Programming Using C++, Fourth edition, Joyce Farrell
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\