Constructors and the Rule of the Big 3

Steven J Zeil

Old Dominion University
Dept. of Computer Science

Table of Contents

1. The Default Constructor
1.1. Book default constructor
1.2. Address default constructor
2. Copy Constructors
2.1. Book - simple arrays
2.2. Book - dynamic arrays
2.3. Shallow vs Deep Copy
2.4. Book - implementing the copy constructor for dynamic arrays
2.5. Book - linked list
2.6. Trusting the Compiler-Generated Copy Constructor
3. Assignment
3.1. Book - simple arrays
3.2. Book - dynamic arrays
3.3. Book - linked list
3.4. Trusting the Compiler-Generated Assignment Operator
4. Destructors
4.1. Book - simple arrays
4.2. Book - dynamic arrays
4.3. Book - linked list
4.4. Trusting the Compiler-Generated Destructor
5. The Rule of the Big 3

Next we turn our attention to a set of issues that are often given short shrift in both introductory courses and textbooks, but that are extremely important in practical C++ programming.

As we begin to build up our own ADTS, implemented as C++ classes, we quickly come to the point where we need more than one of each kind of ADT object. Sometimes we will simply have multiple variables of our ADT types. Once we do, we will often want to copy or assign one variable to another, and we need to understand what will happen when we do so. Even more important, we need to be sure that what does happen is what we want to have happen, depending upon our intended behavior for our ADTs.

As we move past the simple case of multiple variables of the same ADT type, we may want to build collections of that ADT. The simplest case of this would be an array or linked list of our ADT type, though we will study other collections as the semester goes on. We will need to understand what happens when we initialize such a collection and when we copy values into and out of it, and we need to make sure that behavior is what we want for our ADTs.

In this lesson, we set the stage for this kind of understanding by looking at how we control initialization and copying of class values.

In the Forum:

(no threads at this time)