Customizing Copy Constructors in C++

You can customize the copy constructor to perform a deep copy.   

As discussed in the preceding section, the default copy constructor or assignment operator = performs a shallow copy. To perform a deep copy, you can implement the copy constructor.

Listing 11.19 revises the Course class to define a copy constructor in line 11.

Listing 11.19   CourseWithCustomCopyConstructor.h

1 #ifndef COURSE_H
2
#define COURSE_H
3
#include <string>
4
using namespace std;
5
6
class Course
7 {
public:
9    Course(
const string& courseName, int capacity);
10   ~Course();
// Destructor
11   Course(const Course&); // Copy constructor
12   string getCourseName() const;
13   
void addStudent(const string& name);
14   
void dropStudent(const string& name);
15   string* getStudents()
const;
16   
int getNumberOfStudents() const;
17
18
private:
19   string courseName;
20   string* students;
21   
int numberOfStudents;
22   
int capacity;
23 };
24
25
#endif

Listing 11.20 implements the new copy constructor in lines 51-57. It copies courseName, numberOfStudents, and capac i ty from one course object to this course object (lines 53-55). A new array is created to hold student names in this object in line 56.

Listing 11.20 CourseWithCustomCopyConstructor.cpp

1 #include <iostream>
2
#include “CourseWithCustomCopyConstructor.h”
3 using namespace std;
4
5 Course::Course(
const string& courseName, int capacity)
6 {
7    numberOfStudents =
0;
8   
this->courseName = courseName;
9   
this->capacity = capacity;
10   students =
new string[capacity];
11 }
12
13 Course::~Course()
14 {
15   
delete [] students;
16 }
17
18 string Course::getCourseName()
const
19 {
20   
return courseName;
21 }
22
23
void Course::addStudent(const string& name)
24 {
25   
if (numberOfStudents >= capacity)
26    {
27       cout <<
“The maximum size of array exceeded” << endl;
28       cout <<
“Program terminates now” << endl;
29       exit(
0);
30    }
31
32    students[numberOfStudents] = name;
33    numberOfStudents++;
34 }
35
36
void Course::dropStudent(const string& name)
37 {
38   
// Left as an exercise
39 }
40
41 string* Course::getStudents()
const
42 {
43   
return students;
44 }
45
46
int Course::getNumberOfStudents() const
47 {
48   
return numberOfStudents;
49 }
50
51 Course::Course(
const Course& course) // Copy constructor
52 {
53    courseName = course.courseName;

54    numberOfStudents = course.numberOfStudents;
55    capacity = course.capacity;
56    students =
new string[capacity];
57 }

The copy constructor constructs a new array in course2 for storing student names that is independent of the array in course1. The program adds a student “Peter Pan” to course1 (line 10) and a student “Lisa Ma” to course2 (line 11). As you see in the output in this example, the first student in course1 is now “Peter Pan” and in course2 is “Lisa Ma”. Figure 11.7 shows the two Course objects and two arrays of strings for students.

Figure 11.7 After course1 is copied to course2, the students data fields of course1 and course2 point to two different arrays.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *