next up previous
Next: Improvements: Overloaded assignment operator Up: Simple Classes: myvector.h Previous: Simple Classes: myvector.h

Simple vector class

We have seen that a simple array can be used to store the components of a vector. And we have seen that an array is specified by a pointer to the first element of the array. To have a complete specification of an array, however, we also need to say how many components it has. So it makes sense to define an object that packages all the things we need to specify the array. Here is a starting version of our vector class and a main program that exercises it.

class vector {
 private:
  int size;
  double *vec;

 public:
  // Constructors
  vector(int n){ size = n;  vec = new double[size];}

  // Destructor is required because the constructor above uses ``new''
  ~vector() { delete [] vec; }

  // Accessors
  double & operator[](int i){ return vec[i]; }
};

#include <iostream>

int main(){
   int i,n;

   cout << "Enter size of vector\n";
   cin >> n;
   vector a(n);
   cout << "Enter the components\n";
   for(i = 0; i < n; i++)cin >> a[i];
   cout << "Your vector is \n";
   for(i = 0; i < n; i++)cout << a[i] << "\n";

   return 0;
}

Let's examine the new features of this class:




 private:
  int size;
  double *vec;

These are the numeric members of the class. The variable size is designed to hold the length of the vector and vec, the address of the first element of the vector.




  vector(int n){ size = n;  vec = new double[size];}

This is a constructor for the vector. The entire code fits on one line. It requires one argument, namely the length of the vector. The constructor is called in the main program with the declaration vector a(n);. The compiler begins by reserving space for the class members size and vec. Then it stores the value of n in size. Next it uses the new operation to create enough space to hold all of the double precision vector components, and sets vec equal to the address of the first component.

We see that in order for all of this to work, we must know the length of the vector before calling the constructor. That is why the declaration appears in the main program after the input statement that gets the length of the vector.




  ~vector(){ delete [] vec; }

This is a class destructor. All classes have a default destructor. We get it if we don't define one explicitly, but that is not good enough here. The default destructor would simply free storage for the two members size and vec, but would not free storage for the vector elements created by the constructor in the new operation. That is bad, because without releasing this storage, but repeatedly calling the vector constructor, a program could eventually run out of space. The delete [] vec operation frees the storage that was allocated by the new operation in the constructor.




  double & operator[](int i){ return vec[i]; }

Notice that in the main program, we used a[i] to refer to the ith element of the vector as though we were dealing with a simple array. But a is not a simple array. Instead it is an instance of the vector class. So we have to tell the compiler what subscripting means for our vector object. For that purpose, subscripting is elevated to a class method in C++. The compiler names this function operator[]. The definition we supply here overloads the subscript operator, so it does the natural thing when applied to a vector object, namely, it takes an integer argument i (the value enclosed in brackets [], and returns the ith element of the vector, contained in vec[i]. Notice that the return value is a reference to a double, rather than a double. As a reference, it is then possible to store values in the ith element as well as reading values from it.

The subscript operator is defined inside the class declaration, so it is a class method. If a is a vector, the expression a[i] is interpreted by the compiler as a.operator[](i). The latter syntax emphasizes that subscripting on a is regarded as a method of the instance a, called operator[], which takes the integer i as its argument.


next up previous
Next: Improvements: Overloaded assignment operator Up: Simple Classes: myvector.h Previous: Simple Classes: myvector.h
Carleton DeTar 2007-08-17