You are on page 1of 1

Scientific Programming and Computer Architecture

delete vptr;
The class destructor is called when vptr is deleted. If an array is allocated using new[], as in
double *data = new double[size];
it must be released using delete[]:
delete[] data;
The const qualifier
The following definition is contained in the first few lines of Vector.hh (but omitted from earlier listings):
const double PI = 3.1415926535897932384e+00;
Because the definition of PI is qualified using const, any attempt to change the value of PI is illegal. If a
user attempts to change the value of PI, the compiler will catch the error.
The Vector class is equipped with member functions output() and input() to facilitate output to files
and input from files. Definition of the member function input() begins as follows:
void input(const char* fname){...}
Here the argument fname (file name) is a pointer to a char (character). In C and C++, a char is a single
byte with ascii encoding. Strings are arrays of characters terminated by ’\0’ or equivalently the byte of value 0. If
a string is passed as a pointer to a char, its length need not be passed explicitly. The convention for terminating
strings determines its length.
A pointer holds an address which points to some data. Prefixing the const qualifier to the declaration or
definition of a pointer implies that the data does not change, but the pointer may change. So it is illegal to say
fname[0]=’M’ within the body of the function, but it is legal to say char c; fname = &c;.
Suppose we want to initialize a vector of length 20 using data stored in a file named init.dat in the current
directory. The data in the file must be a sequence of 20 (or more---the extra values are ignored) values separated by
whitespace. The following code does that:
1 char fname[9]={’i’,’n’,’i’,’t’,’.’,’d’,’a’,’t’,’\0’};
2 Vector v(20);
3 v.input(fname);
In line 1, the number 9, which gives the length of the array, can be omitted because the compiler can use the
initializing sequence to determine the length. The usage below is much more convenient.
Vector v(20);
v.input("init.dat");
We are allowed to pass “init.dat” as an argument explicitly because the first argument of the member
function input() is of the type const char * and not just char *.
Use of const qualified references is illustrated by the member function add().
void add(const Vector& v){...}
Here the const qualifier indicates that add() will read entries of v but not change them. The target will be
changed when v is added to it. The usage of the const qualifier in the member function norm() is different.

double norm()const{...}
This function returns the ∞-norm of its target vector (largest magnitude of an entry of the vector). Therefore,
if it is called v.norm(), the returned value is the ∞-norm of v. The const qualifier specifies that the member
function will not change its target. Any attempt to change the entries of v inside the definition of norm() is illegal.
The const protections can be easily broken using shadows. For example, inside the definition of the
member function add(), we can say
Vector w(v);
and go on to modify the entries of v.
Default arguments
Unlike C, C++ allows default arguments. Suppose the function

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

You might also like