Introduction to C++ (Part 2)

© 2000 Krishna Myneni



Arithmetic

The intrinsic types, char, int, long, float, and double, are fixed size containers for holding numbers. Table 1 shows the allowed range of numbers for each type on a 16-bit system.


Table 1: Allowed number range for each type on a 16-bit system
Type Range
char -127 to 127
unsigned char 0 to 255
int -32,768 to 32,767
unsigned int 0 to 65,535
long -2,147483,648 to 2,147,483,647
unsigned long 0 to 4,294,967,295
float -3.4$\times$10$^{38}$ to -3.4$\times$10$^{-38}$
  3.4$\times$10$^{-38}$ to 3.4$\times$10$^{38}$
double -1.7$\times$10$^{308}$ to -1.7$\times$10$^{-308}$
  1.7$\times$10$^{-308}$ to 1.7$\times$10$^{308}$


Because their sizes are finite and different from type to type, several side-effects, such as wrapping and loss of precision, may occur when performing arithmetic. These are illustrated in the program arithm.cpp.

Pointers

A pointer is a variable that holds the location of another variable. Pointers are separate data types, with a correspondence to the intrinsic types (or to user defined types). For example, a pointer to a variable of type int is declared by

    int *p;

The variable p holds the location (or address) of an integer. The address of a variable may be obtained by the referencing operator, also known as the address of operator, denoted by the symbol &. A pointer can be assigned the address of a variable in the following manner.

    int i = 5;
    int *p = &i;

The referencing operator, also known as the indirection operator, is denoted by the symbol *. When applied to a pointer, it references the quantity held at the location given by a pointer. Adding to the example above.

    cout << "The value of i = " << *p;

Pointers may be used to pass arguments by reference in function calls. A few examples are given in the program ptrfun.cpp. C++ also allows arithmetic to be performed on pointers. This feature provides a very efficient method of working with arrays, also illustrated in the sample program.

Note: The value of a pointer variable does not necessarily indicate a physical address in memory.

I/O Streams

C++ provides a standard class library for dealing with input and output. The stream classes can be used for i/o to the terminal, to files, and to strings.

A class encapsulates data and functions, and can be thought of as a miniature program. Classes can also derive their properties from other classes, a feature known as inheritance. Figure 1 shows a partial diagram of the stream classes.

Figure 1: A Partial Diagram of the Stream Classes

The class ios is the base class for i/o streams. A stream is an abstract concept which includes a data source, a data sink, and a buffer connecting the two. The ios class provides many functions for controlling the format of input and output, and for error checking. Some of the member functions of the ios class are

int bad();
int eof();
int fail();
int clear();
char fill (char);
int precision (int);
long setf (long);
int width (int);

The classes istream and ostream are used for input and output to and from a stream. The istream class defines an operator for extracting data from the stream. The extraction operator is denoted by $>>$. The ostream class defines an operator for inserting data into a stream. The insertion operator is denoted by $<<$. The standard library provides the predefined objects cout and cin.

The ifstream and ofstream classes create streams between the user's program and files. Examples of their use are given in the programs streams.cpp, binfile.cpp, and bintest.cpp. The examples illustrate both formatted (text) i/o, and unformatted (binary) i/o to files.

Dynamic Memory Allocation

Memory for variables and arrays can be allocated by a program when it is executing, i.e. at run-time. C++ provides two operators for dynamic allocation of memory: new and delete. For example, an array of integers may be allocated within a program by the following statement

    int *p = new int [500];
The pointer p points to the first element in the 500 element array of integers. The size of the array may be a variable that is set in the course of program execution. For each use of the new operator, there must be a corresponding use of delete.
     delete [] p;
C++ does not automatically dispose of dynamically allocated memory when the program is finished. Dynamic memory allocation is illustrated in the program binfile.cpp.