Computational Physics - Department of Physics

(Axel Boer) #1

3.3 Classes in C++ 73


Our classVectorhas as data a plain one-dimensional array. We define several functions
which operate on these data, from subscripting, change of the length of the array, assignment
to another vector, inner product with another vector etc etc. To be more specific, we define the
following usage of our class,that is the way the class is usedin another part of the program:



  • Create vectors of a specified length defining a vector asVector\ v(n);Via this statement
    we allocate space in memory for a vector withnelements.

  • Create a vector with zero length by writing the statementVector v;

  • Change the dimension of a vectorvto a given lengthnby declaringv.redim(n);. Note
    here the way we use a function defined within a class. The function here isredim.

  • Create a vector as a copy of another vector by simply writingVector v(w);

  • To extract the length of the vector by writingconst int n = v.size();

  • To find particular value of the vectordouble e = v(i);

  • or assign a number to an entry viav(j) = e;

  • We would also like to set two vectors equal to each other by simply writingw = v;

  • or take the inner product of two vectors asdouble a = w.inner(v);or alternatively
    a = inner(w,v);

  • To write out the content of a vector could be done by viav.print(cout);


This list can be made longer by adding features like vector algebra, operator overloading etc.
We present now the declaration of the class, with our comments on the various declara-
tions.


classVector
{
private:
double*A; // vector entries
int length; // the length ofthe vector
void allocate (intn);// allocate memory, length=n
void deallocate(); // free memory
public:
Vector (); // Constructor, use as Vector v;
Vector (intn); // use as Vector v(n);
Vector (constVector& w);// us as Vector v(w);
~Vector (); // destructor to clean up dynamic memory
boolredim (intn); // change length, us as v.redim(m);
Vector&operator= (constVector& w);// set two vectors equal v = w;
double operator() (inti)const;// a = v(i);
double&operator() (inti); // v(i) = a;
voidprint (std::ostream& o)const;// v.print(cout);
doubleinner (constVector& w)const;// a = v.inner(w);
intsize ()const {returnlength;}// n = v.size();
};


The class is defined via the statementclass Vector. We must first use the key wordclass,
which in turn is followed by the user-defined variable name. The body of the class, data and
functions, is encapsulated within the parentheses...;.
Data and specific functions can be private, which means that they cannot be accessed from
outside the class. This means also that access cannot be inherited by other functions outside
the class. If we useprotectedinstead ofprivate, then data and functions can be inherited
outside the class. The key wordpublicmeans that data and functions can be accessed from
outside the class. Here we have defined several functions which can be accessed by functions
outside the class.
The first public function we encounter is a so-called constructor, which tells how we declare
a variable of typeVectorand how this variable is initialized

Free download pdf