Computational Physics - Department of Physics

(Axel Boer) #1

3.3 Classes in C++ 81


/
Division of the entries of a vector by a scalar.
/
friendVectoroperator/(constVector&,double);// u = v/a
// dot product
friend doubleinner(constVector&,constVector&);
/

print the entries of a vector to screen
/
friendstd::ostream&operator<<(std::ostream&,constVector&);// cout << v
// Note: This function does not need access to the data
// member. Therefore, it could have been declared as a not friend.
};


//
/
INLINE FUNCTIONS /
/
/


// Destructor
inlineVector::~Vector(){delete[] vec;}


// Get the number of entries in a vector
inline intVector::getLength()const {return length;}


/
@return a constant pointer to the array of data.
This function can be used to interface C++ with Fortran/C.
/
inline const double*Vector::getPtr()const {returnvec;}


/
@return a pointer to the array of data.
This function can be used to interface C++ with Fortran/C.
/
inline double*Vector::getPtr(){returnvec;}


// Subscript. 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].
// The return value "const double&" is equivalent to
// "double", with the difference that the first approach
// is preferible when the returned object is big.
inline const double& Vector::operatorconst{
#ifdef CHECKBOUNDS_ON
indexOk(i);
#endif
returnvec[i];
}// read-only the ith component of the vector.
// const at the end of the function declaration means
// that the caller code can just read, not modify


// Subscript. (DANGEROUS)
inline double& Vector::operator{
#ifdef CHECKBOUNDS_ON
indexOk(i);
#endif
returnvec[i];
}// read-write the ith coordinate

Free download pdf