C++ Classes


An Example C++ Class

C++ classes are similar to Java classes in many ways, but there are also important differences. Below is an example of a C++ class named IntList to be used to represent a list of integers; operations to add a value to the end of the list and to print the list are provided. The implementation uses a dynamically allocated array to store the integers; when the array is full, a new array of twice the size is allocated.

The code that defines the class would be split into two files: the first part specifies what member functions (methods) and data members (fields) the class will have. That code goes into a header file: a file with the extension .h. It is usually a good idea (though not a requirement as in Java) to give the file the same name as the class (so the file would be named IntList.h).

The second part of the class definition gives the code for the function bodies. That code goes in a source file (e.g., IntList.C).

The reason for splitting up the code is that it is generally a good idea to try to separate the interface from the implementation. Someone who wants to use an IntList really only needs to know what IntList operations are available; it is not necessary to know all the details about how an IntList is implemented. However, splitting up the code in this way is not required by C++. Some people prefer to include code for the member functions in the .h file when that code involves only one or two statements.

Here is the code that would be in IntList.h:

Things to note about the example so far: Here is the code that would be in IntList.C (the actual code for the AddToEnd and Print functions has been omitted): Things to note about this part of the example:

Constructor Functions

As in Java, constructor functions can be overloaded (there can be multiple constructors for a class, as long as each has a different number and/or type of parameters). In C++, a constructor function is called either when a class object is declared:

or when the object is dynamically allocated:

To use a constructor with parameters, just put the values for the parameters in parentheses as follows:


  1. Extend the IntList class defined above by adding a member function called Length. The function should return the number of items currently in the list. Write the new declaration that would be added to IntList.h as well as the new code that would be added to IntList.C (write the complete code for the new function , not just ... as in the example).

  2. Add a 2-argument constructor to the IntList class to allow an IntList to be initialized to contain n copies of value v. (So the two arguments are n and v, both of type int.) Again, write both the new declaration that would be added to IntList.h, and the new code that would be added to IntList.C.

Two Useful Standard Classes: string and vector

The string class

To use the string class you must #include <string> (be sure that you do not include string.h, because then you will get the header file for C-style strings rather than for the C++ string class).

The vector class

To use the vector class you must #include <vector>. A vector is similar to an array, but vectors provide some operations that cannot be performed using C++ arrays, and vectors can be passed both by value and by reference (unlike C++ arrays, which are always passed by reference). Unfortunately, there is no bounds checking for vectors (i.e., an index out of bounds does not necessarily cause a runtime error).


  1. Write a function named NonEmpty that has one parameter, a vector of strings V, and that returns another vector of strings that contains just the non-empty strings in V. For example, if parameter V contains the 6 strings: then function NonEmpty should create and return a vector that contains the 3 strings:

  2. Write a function named Expand that has one parameter, a vector of ints V. Expand should change V so that it is double its original size, and contains (in its first half) the values that were originally in V.

    Test your function with the following main function:

    When you run this program, the output should be:

Classes with Pointer Data Members

Every class that has a pointer data member should include the following member functions:

  1. a destructor,
  2. a copy constructor,
  3. operator= (assignment)
The IntList class, defined above, includes a pointer to a dynamically allocated array. Here is the declaration of the IntList class again, augmented to include declarations of the class's destructor, copy constructor, and assignment operator (in red for emphasis):

Destructor Functions

An object's destructor function is called when that object is about to "go away"; i.e., when:

  1. a class object (a value parameter or a local variable) goes out of scope, or
  2. a pointer to a class object is deleted (the dynamically allocated storage pointed to by the pointer is freed by the programmer using the delete operator)
The main purpose of the destructor function is to free any dynamically allocated storage pointed to only by a data member of that object. (Note that it is up to the programmer to ensure that no other pointers are pointing to that storage.)

For example, consider the following function, with line numbers included for reference:

In this example, the scope of value parameter L is the whole function; L goes out of scope at the end of the function (line 8). So when function f ends, L's destructor function is called. (Note: if f had one or more return statements, L's destructor function would be called when a return was executed).

The scope of variable L1 is the body of the while loop (lines 4 to 6). L1's constructor function is called at the beginning of every iteration of the loop, and its destructor function is called at the end of every iteration of the loop. Note that if the loop included a break or continue statement, the destructor would still be called.

Variable p is a pointer to an IntList. When an IntList object is allocated using new at line 2, that object's constructor function is called. When the storage is freed at line 7, the object's destructor function is called (and then the memory for the Intlist itself is freed).


Why isn't the destructor function of a reference parameter called at the end of the function?

Destructor functions are defined using syntax similar to that used for the constructor function (the name of the class followed by a double colon followed by the name of the function). For example, the definition of the Intlist destructor function would look like this:

NOTE: If you don't write a destructor function for a class that includes pointers to dynamically allocated storage, your code will still work, but you will probably have some storage leaks.

To understand more about storage management and destructor functions, let's consider a simpler version of the example code give above:

Assume that just before line 4, we have the following situation: If there is no IntList destructor, then when delete p is executed, the storage for the IntList object pointed to by p (which was alloacted at line 2) is freed. However, the array pointed to by the IntList's Items field is not freed, and will never be freed, so that is a storage leak. If the IntList destructor given above (that deletes the array pointed to by Items) is provided, then it is called when line 4 is executed. That call frees the array storage, and then the delete operator frees the storage pointed to by p (namely, the storage for the IntList itself), and there is no storage leak.

Copy Constructor Functions

An object's copy constructor is called (automatically, not by the programmer) when it is created, and needs to be initialized to be a copy of an existing object. This happens when an object is:

  1. passed as a value parameter to a function,
  2. returned (by value) as a function result,
  3. declared with initialization from an existing object of the same class.
The purpose of the copy constructor is to make a copy of the
  1. actual parameter,
  2. value being returned,
  3. existing object.


If you don't write a copy constructor, the compiler will provide one that just copies the value of each data member. If some data member is a pointer, this causes aliasing (both the original pointer and the copy point to the same location), and may lead to trouble. For example, consider the following code:

If the IntList class does not include a copy constructor, the compiler will supply one that just copies the value of the pointer Items. Here are pictures illustrating the result of the call to I's copy constructor, which initializes the formal parameter L to be a copy of I. Note that both I.Items and L.Items point to the same array. Now think about what happens when the body of function f executes. L.AddToEnd discovers that the array is full, so it allocates a new array, copies the values from the old array to the new array, and returns the old array to free storage. Unfortunately, L.AddToEnd doesn't know that I.Items is also pointing to the old array, so when that array is returned to free storage, I.Items becomes a dangling pointer, and any attempt to access the array it points to is likely to lead to trouble.


Consider the StrList class defined below. A StrList stores a list of strings in a linked list pointed to by the StrList's head field. The Lookup operation determines whether a given string is in the list; if it is there, it is moved to the front of the list, and the value true is returned (otherwise, the list is unchanged, and the value false is returned).

Consider the following code: Note that there is no StrList copy constructor (so the compiler will supply one). Draw variables S and L as they would appear at the very beginning of function f (just after L's copy constructor is called to initialize it to be a copy of S). Draw a second picture to illustrate what happens as a result of the call to L.Lookup in function f. What goes wrong because there is no StrList copy constructor?

The Copy Constructor Declaration

Recall that the declaration of a class's copy constructor is similar to that of its default (no-argument) constructor: the function has no return type (not even void), and its name is the same as the name of the class. However, unlike the default constructor, the copy constructor has one argument: its type is the class, and it is a const reference parameter. The argument is the object that the copy constructor is supposed to copy. For example:

The Copy Constructor Definition

The definition of the copy constructor (the actual code for the function) should be put in a ".C" file, along with the code for the other class member functions. The copy constructor should copy the values of all non-pointer data members, and should copy the objects pointed to by all pointer data members. For example, the copy constructor for the IntList class should perform the following tasks:

  1. allocate a new array of ints of size L.arraySize (L is the copy constructor's IntList parameter); set Items to point to the new array;
  2. copy the values in the array pointed to by L.Items to the new array;
  3. initialize the numItems and arraySize fields to have the same values as the ones in L.numItems and L.arraySize.
Here is the code for the IntList copy constructor (note that, like the other constructor functions, the copy constructor can use a member initialization list to initialize data members, as well as using code in the body of the function):


In C++ you can assign from one class object to another (of the same type). For example:

By default, class assignment is just field-by-field assignment. For example, the above assignment is equivalent to: (Of course, the three field assignments could not be written outside an IntList member function, since they are private fields; however, they illustrate the effect of the assignment L1 = L2.)

If a class includes pointer fields, the default class assignment causes aliasing, and as we have seen in the case of the copy constructor, this can lead to trouble! For example, if the L2.Items array is full when the assignment is done, then a subsequent call to L1.AddToFront will cause the array to be returned to free storage (so L2.Items will become a dangling pointer).

The default assignment can also cause storage leaks when the class has a pointer field. For example, when L1 = L2; is executed, L1.Items is simply overwritten with the value in L2.Items, the array that L1 was pointing to is not returned to free storage (and that storage is now lost).

To prevent these problems, you should always define operator= as a class member function for a class with a pointer field. The declaration of the member function looks like this for the IntList class:

The idea is that when the assignment L1 = L2; is executed, L1's member function operator= is called, and L2 is passed as the argument to that function.

Note that IntList's operator= function returns an IntList. This is to permit chained assignment, for example: L1 = L2 = L3;. When this statement is executed, the expression L2 = L3 is evaluated first; the result of evaluating that expression is used as the right-hand side of the assignment to L1. The operator= function returns its result by reference (that's what the ampersand means). This is done for efficiency, to prevent the IntList copy constructor being called to make a copy of the returned value.

Note that operator= differs from the copy constructor in three important ways:

  1. The object being assigned to has already been initialized; therefore, if it has a pointer field, the storage pointed to must be freed to prevent a storage leak.

  2. It is possible for a programmer to assign from a variable into itself; for example: L1 = L1. The operator= code must check for this case, and do nothing.

  3. The operator= code must return a value.

Here is the definition of operator= for the IntList class:

Note that, as in Java, every member function has access to a variable named this that is a pointer to the object whose member function was called. So for example, when L1 = L2; is executed, L1's member function operator= is called, so this is a pointer to L1.

To check whether the assignment was L1 = L1, we compare the pointer this with the address of the parameter, L; in the case of L1 = L1, the parameter is L1, so its address is the same as the address that is the value of this. Be sure to include this test every time you write an operator= function!

We also make use of this for the returned value; the type to be returned is IntList (not pointer to IntList) so we return *this (the IntList pointed to by this) rather than plain this.


Every class that has a pointer data member should include the following member functions:

  1. a destructor,
  2. a copy constructor,
  3. operator= (assignment)
If you don't write a destructor, your code will probably still work, but it may have storage leaks (some uses of the new operator will have no corresponding use of delete).

If you don't write a copy constructor, or you don't write operator=, your code may not work correctly; there may be attempts to dereference dangling pointers (which may cause runtime errors, or may cause garbage values to be assigned to some variables), and/or some data may be lost or corrupted.

A kind of compromise is to forbid the use of the copy constructor and the assignment of two class objects. You can do this by declaring the copy constructor and operator= as private member functions (just declaring them is enough; you do not need to write the actual code). In this case, any code that would normally cause the copy constructor or operator= to be called will instead cause a compile-time error.