82 3 Numerical differentiation and interpolation
// Alternative to operator[]
inline const double& Vector::operator()(inti)const{
#ifdef CHECKBOUNDS_ON
indexOk(i);
#endif
returnvec[i];
}// read-only the ith component of vec
// Subscript (DANGEROUS). If v is an object of type Vector, the ith
// component of v can be accessed as v(i) closer to the
// ordinary mathematical notation instead of v.vec(i).
inline double& Vector::operator()(inti){
#ifdef CHECKBOUNDS_ON
indexOk(i);
#endif
returnvec[i];
}// read-write the ith component of vec
/**/
/ (Arithmetic) Unary operators /
/**/
// Unary operator +
inlineVectoroperator+(constVector& v){// u = + v
returnv;
}
// Unary operator -
inlineVectoroperator-(constVector& v){// u = - v
returnVector(v.length) -v;
}
#endif
Finally, we list the source codes not included in the header file (all function which are not
inlined)
http://folk.uio.no/mhjensen/compphys/programs/chapter03/cpp/Vector.cpp
#include"Vector.h"
/*
@file Vector.cpp
@class Vector
@brief Implementation of class used for manipulating one-dimensional arrays.
**/
// default constructor
Vector::Vector(){
length = 0;
vec = NULL;
}
// constructor
Vector::Vector(int_length){
length =_length;
vec =new double[_length];
for(inti=0; i<_length; i++)
vec[i] = 0.0;
}
// Declare the array to be constant because it is passed
// as a pointer. Hence, it could be modified by the calling code.