OOP Lab 5 - Abstraction III
OOP Lab 5 - Abstraction III
OOP Lab 5 - Abstraction III
(LAB-05)
Abstraction III
(Shallow Copy / Deep Copy and 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 50
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.
Capital University of Science and Technology, Islamabad Page 43
Department of Computer Science (2017)
Lab 5 – Abstraction III
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.
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
};
Capital University of Science and Technology, Islamabad Page 45
Department of Computer Science (2017)
Lab 5 – Abstraction III
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
{
for(int i=0; i<=9;i++)
{
a[i]=0; // put the value 0 in all locations
}
}
// 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]; //size can be passed from main to constru
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
int main()
{
const int size=10;
example obj1[size]=example(size);
//array of objects initialized with parameterized constructor
example obj2;
simple class with a single data member of your choice. Create a private constructor.
In your code demonstrate how a private constructor is called and how the object is created using a private
constructor.
6. Procedure & Tools
6.1 Tools
Visual Studio 2008.
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
Capital University of Science and Technology, Islamabad Page 48
Department of Computer Science (2017)
Lab 5 – Abstraction III
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
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 = Red After selective copying only the permanent attributes contain
Owner = Ali Raza values.
Year of manufacture = 2013 Colour = Red
Cubic Capacity = 5700 Owner =
Engine number = 123456 Year of manufacture =
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$\