Scientific Programming and Computer Architecture 28

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Scientific Programming and Computer Architecture

programs may include either assert.h or cassert, as on line 3, to emphasize that a C facility is being employed.
The Vector class uses other C facilities as well. It uses the fabs() function from cmath (line 4) in one of
its member functions to compute the norm of the vector. C facilities to allocate and release memory are defined in
stdlib (line 5).
C++ facilities for input/output from terminals and files are in iostream (line 7) and fstream (line 8),
respectively. Member functions that input/output vectors to/from files use these facilities.
To output a variable x to standard output (typically the terminal), we may say
cout<<x<<endl;
Here cout is the name of standard output declared in iostream. The end of line character endl is also
declared in iostream. In C, input/output syntax is sensitive to the type of data items being handled. The abstraction
features of C++ are used by the iostream library to provide a uniform interface for input/output regardless of the
type of the variable. Even class objects may be input/output in this manner if the operators << (for output) and >>
(for input) are overloaded suitably.
To output to a file, the syntax looks as follows:
ofstream ofile("tmp.txt");
ofile<<x<<endl;
Here ofile is defined as an ofstream object. At the point of definition, it is tied to the file tmp.txt.
To input x from standard input, we may say
cin>>x;
To input from a file, we may say
ifstream ifile("tmp.txt")
ifile>>x;
This will work regardless of whether x is a double or an int or a long or a char.
The class names ofstream and ifstream as well as class object names cout and cin are defined in the
namespace std. In general, we should say std::cout and std::ofstream because these names do not exist
outside the namespace. However, the using namespace declaration on line 10 brings in all the names in std into
scope. It allows us to say cout instead of std::cout.
It is often not a good idea to bring in the entire std namespace, especially within header files. The C++
standard library is vast. Bringing in the entire std namespace, as we do on line 10, pollutes the namespace
considerably. For example, a programmer may define a function called copy() to copy double arrays in a
program-specific manner and conflict with the std namespace. There are functions with common names such as
copy() and sort() in the standard library.
The listing shows the outline of the definition of the class Vector (lines 12 through 17). There is a private
section (line 13) in the class definition and a public section (line 15). The class definition must end with a
semicolon as on line 17. Omitting the semicolon at the end of class definitions is a common novice error.
Private section of the Vector class

Figure 1.5 Schematic picture of a Vector of length 2, which ‘‘owns’’ its data.

A class is a collection of data members and member functions. In the Vector class all the data members are
in the private section.
class Vector{
private:

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

You might also like