Loading

C++: Constructors and Destructors

Constructors and Destructors

In this video, we cover constructors and destructors for C++ objects. Constructors give a means to initializing newly created objects and can be overloaded. Destructors offer a way to perform clean up on an object just before it is destroyed; a class can have at most one destructor. Together constructors and destructors provide a method of encapsulating the lifespan of an object. Although we have not done anything useful with destructors so far, we will see that they are extremely useful as our programs get more complex.

In mathematics, an ordered list numbers is called a sequence. Sequences come in many types, but we can model a generic sequence with a class. Sequences can be of any length, but we will put a limit of 100 terms on our sequences for this first example:

The code above describes a sequence class with two constructors, a member function, and two data members. The first constructor is a default constructor, which creates a sequence with no terms in it. The second constructor takes an array of terms and a length; the terms are copied from the array to the sequence via a for-loop. The member function outputs the length of the sequence and then all of the terms—a check is made internally to assure that sequence has at least one term before outputting them. The data members are the length and an array of doubles to hold the terms.

In this main function, we create two instances of our sequence object and call Print() to output the sequences to the console window.

Above, we have the output generated from executing this program.

There are many types of sequences used in mathematics. Our next class will demonstrate one of the most come types of sequences: Arithmetic.

An arithmetic sequence has a first term and a common difference. Successive terms are generated from previous term by adding the common difference.

In the class above, we have a class that represents an arithmetic sequence. Since we have a formula to generate terms of the sequence, we do not need to store them in an array as we did before. Instead, we can represent an infinite number of terms with just an initial term and a common difference as data members.

Additionally, our class has two constructors and a destructor. As in the video, this destructor is not essential, but rather is used for illustration. It will be a bit before we see example of a practical destructor use. For now, just note how and when the destructor is called.

The code above creates two arithmetic sequence objects: one with each constructor. After the sequence is created, we output a single term for illustration. The default constructor creates the sequence 0, 1, 2, 3, . . . . The second constructor creates that sequence 5, 8, 11, 14, . . . . The destructors are called automatically when the main() function returns.

Above, we have the output from the execution of this second example. The first two lines are single terms that were printed from each sequence. The third and fourth lines are output generated from the destructor calls. Observe that the destructors for both sequences were called after the terms were printed. Another interesting thing to observe is that the destructor for the second sequence was called before the destructor for the first sequence. We will explain why this happens in a later lesson.