You are on page 1of 1

Scientific Programming and Computer Architecture

long int size;


double *data;
int owner;
public:
...
};
The class member size is the length of the vector and data is a pointer to the contents of the vector; so
data[i] is in effect a name for the ith entry of the vector. The owner field will be either 0 or 1---its meaning is
explained later.
If v is a variable of type Vector, then it is a name for a segment of memory that includes a location of type
int, a location of type double *, and a location of type int. A schematic view of a Vector of length 2 is shown
in figure 1.5↑. If v is the name of an object of class Vector, v.size, v.data, and v.owner are names for members
of v as shown in the figure. One may say that the class object v is a package of the data items v.size, v.data, and
v.owner.
If a data member or a member function is in the private section, its access is restricted. Because the data
items v.size, v.data, and v.owner are in the private section, only member functions are allowed to access
them. [14]  Whatever functionality we want must be defined through the member functions. For example, suppose we
want to input a vector from a file. We are not allowed to directly set the size of the Vector object during input
using syntax such as v.size=100. Instead, we may define a member function input(), which takes care of
reading data and setting v.size appropriately.
Member functions of the Vector class
All the member functions of the Vector class are in the public section.
1 class Vector{
2 private:
3 ...
4 public:
5 Vector(){...}
6 Vector(long int n){...}
7 Vector(const Vector& v){...}
8 ~Vector(){...}
9 void shadow(double *dataptr, long int len)
10 {...}
11 void shadow(const Vector& v){...}
12 void shadow(const Vector& v, long int i,
13 long int len){...}
14 long int getSize() const{...}
15 double * getRawData() const{...}
16 double& operator()(long int i){...}
17 const double& operator()(long int i) const{.}
18 Vector& operator=(const Vector& v){...}
19 void add(const Vector& v){...}
20 void sub(const Vector& v){...}
21 void mul(const Vector& v){...}
22 void div(const Vector& v){...}
23 void scale(const double x){...}
24 void add_constant(const double x){...}
25 double norm() const{...}
26 void output(const char* fname)const{...}
27 void input(const char* fname){...}
28 };
There is a basic difference between data members and member functions. Suppose v is a Vector class
object. Then v.size, v.data, and v.owner refer to the data items packaged inside v. In contrast, v.norm()
applies the member function norm() (line 25) with the class object v as its target.
The member functions exist within the namespace defined by the class. Each class defines a namespace. The

https://divakarvi.github.io/bk-spca/spca.html[20-1-2019 23:44:49]

You might also like