Vector Class Implementation

Now that we have learned how to do copy control, how to create a class, and how to use pointers, let us create one of the very first class we used: vectors . Let us begin to create this class.

First off, what is a vector? In the past, we know that we used vectors to store a collection of data. We also know that we used various methods (such as push_back, pop_back, etc.... ). A vector is composed of 3 main parts: a data pointer pointing to an array, something to keep track of the size, and something to keep track of the capacity (note that size and capacity are not the same; the capacity is how much data the collection can hold at present, the size is how much data the collection currently holds). In order to create our vector, we will then need to have these 3 items in our constructor. We will also need to recreate the vector methods that we used before for our own vector class. In other words, we will create our own vector class by using the same vector method names but we will supply our own function bodies to them.

Here are some of the items we will need to implement:

You might be wondering, "hey we can make vectors that hold any type. How do we do that?" We do not want to create a data pointer with all the different types because it is space inefficient. Also, there is another problem: how will also account for pointers to new classes we want to make? In other words, we do not want to recreate the same vector methods to account for all the classes we will use in for our vector. The easy way of doing this will be discussed in the further topic of templates.