Lab 10: Operator Overloading

Topics

Container Classes

Container Classes are classes that maintain collections of objects.

Example:

class PersonContainer
{
   public:
      ...

   private:
      Person* array;
      ...
}           

The operations on container classes can be:

Three concepts of container class implementation:

Usually, there is a strong coupling between the three concepts.

Generic Programming

Generic programming is a method through which we can remove (or greatly reduce) the strong coupling between containers, container elements and operations on the elements of the container.

Generic Programming is to program with types as parameters.

We will learn more on this in the next lab.

Operator Overloading

A few points to be noted:

A tutorial on operator overloading

Lab Task

Here is a class Array with boundary checking in Array.h, Array.cpp and main.cpp. It is a type-generic array, where type T is currently defined as int. The class is already almost implemented for you, but most codes are poorly organized such that the implementation part is mixed with the declaration part. Your tasks in this lab are

  1. Implement the operator overloading for the operators  =,== and [] declared in Array.h and put them in Array.cpp, where the meanings of the operators have been given in the files.
  2. In order to merge two string arrays together in merge.cpp, please implement one more member function Array& operator+=(const Array& arr); which appends an Array object to the end of another Array object.

Note: