next up previous
Next: About this document ... Up: Simple Classes: myvector.h Previous: Improvements: More constructors

myvector.h file

We now summarize our definition of the vector class, This is the file myvector.h:



// An abbreviated, primitive double precision vector class
// C. DeTar 9/26/03
// Physics 6720 University of Utah
#include <iostream>

class vector {
 private:
  int size;
  double *vec;    // Note that only a pointer is kept here
  // Auxiliary function for internal range checking
  int range(int i);
  
 public:
  // Constructors 
  vector(int);
  vector(const double*, int);
  vector(const vector&);
  
  // Destructor is required because the constructors above use "new" 
  ~vector() { delete [] vec; }
  
  // Accessors
  double & operator[](int i){ return vec[range(i)]; }
  
  // Unary operator +
  vector & operator+() { return *this; }
  
  // Assignment =
  vector & operator=(const vector &v);
  
  // Compound assignments
  vector & operator+=(const vector &);
  vector & operator-=(const vector &);
  vector & operator*=(const double x);
  vector & operator/=(const double x);
  
  // Binary +
  friend vector operator+(const vector &v1, const vector &v2);
  
  // Binary -
  friend vector operator-(const vector &v1, const vector &v2);
  
  // Multiply by real (double) on left
  friend vector operator*(const double x, const vector &v);
  
  // Multiply by real (double) on right
  friend vector operator*(const vector &v, const double x);
};

// Private auxiliary function for range checking
inline int vector::range(int i)
{
  if(i < 0 || i >= size)
    {
      cerr << "FATAL vector::range(" << i << ") out of bounds [0," 
	   << size-1 <<"]\n";
      exit(1);
    }
  return i;
}

inline vector::vector(int n)
{
  int i;
  
  size = n;
  
  // "new double" allocates storage for an array of doubles and
  // returns a pointer to the beginning of the array.
  vec = new double[size];
  // If the allocation failed, vec is returned NULL.
  if(vec==NULL)
    {
      cerr << "FATAL vector::vector(int) can't allocate vector(" 
	   << n << ")\n";
      exit(1);
    }
  for(i = 0; i < size; i++)vec[i] = 0.;
}

// Class constructor allowing declarations of the form
// vector b(a,10);
// where we start with
// double a[10];  (simple array - not a class instance!)
// and initialize a[10];

inline vector::vector(const double *a, int n){
  size = n;
  vec = new double[size];
  // If the allocation failed, vec is returned NULL.
  if(vec==NULL)
    {
      cerr << "FATAL vector::vector(const double *, int) can't allocate vector(" 
	   << n << ")\n";
      exit(1);
    }
  for(int i = 0; i < size; i++){
    vec[i] = a[i];
  }
}

// Copy constructor
// Class initializer allowing declarations of the form
// vector b(10);
// Initialize b -- then
// vector b = a;

inline vector::vector(const vector &v){
  size = v.size;
  vec = new double[size];
  // If the allocation failed, vec is returned NULL.
  if(vec==NULL)
    {
      cerr << "FATAL vector::vector(const vector &) can't allocate vector(" 
	   << v.size << ")\n";
      exit(1);
    }
  for(int i = 0; i < size; i++)vec[i] = v.vec[i];
}

// Assignment =
inline vector & vector::operator=(const vector &v){
  if(size != v.size){
    cerr << "FATAL vector::operator=(const vector &) size mismatch: " <<
      size << " " << v.size << "\n";
    exit(1);
  }
  for(int i = 0; i < size; i++)vec[i] = v.vec[i];
  return *this;
}

// Add and assign +=
inline vector & vector::operator+=(const vector &v){
  if(size != v.size){
    cerr << "FATAL vector::operator+=(const vector &) size mismatch: " <<
      size << " " << " != " << v.size << "\n";
    exit(1);
  }
  for(int i = 0; i < size; i++) vec[i] += v.vec[i];
  return *this;
}

// Binary operator +
inline vector operator+(const vector &v1, const vector &v2){
  if(v1.size != v2.size){
    cerr << "FATAL vector::operator+(const vector &, const vector&) size mismatch: ";
    cerr << v1.size << " " << " != " << v2.size << "\n";
    exit(1);
  }
  
  vector v(v1.size);   // Construct a temporary vector to hold the sum
  for(int i = 0; i < v1.size; i++)
    v.vec[i] = v1.vec[i] + v2.vec[i];
  return v;
}

This header file lacks several definitions:

Since we already have quite similar operators, we leave it as an exercise for the reader to supply them and complete the file myvector.h.



Carleton DeTar 2007-08-17