Reference: C++ Programming: From Problem Analysis to Program Design, 2nd Ed. by D. S. Malik
Charles E. Oyibo
A C++ program is a collection of one or more subprograms, called functions. Roughly speaking, a subprogram or a function is a collection of statements that accomplishes something when its is activated, or executed. Some functions, called predefined or standard functions are prewritten and provided as a part of software development kits (SDKs) like Visual C++ and Borland C++. Every C++ program has a function called main; thus if a C++ program has only one function it must be called main.
Some definitions are in order at this point:
Syntax rules tell you which statements (instructions) are legal, or accepted by the programming language, and which are not
Semantic rules determine the meaning of the instructions
The smallest individuaul unit of any language is called a token. C++'s tokens are divided into special symbols, word symbols, and identifiers.
Special Symbols include:
- Mathematical symbols + - * /
- Punctuation marks taken from English grammar .
; ? ,
- Relational symbols <= != == >=
- Blank symbol (obtained by pushing the spacebar once)
Word symbols include: int, float, double, char, void, return. Word symbols are also called reserved words, or keywords. The letters that make up a reserved words are always lowercase, and are considered to be a single symbol. Futhermore, word symbols cannot be redefined within any program.
Identifiers are names of things that appear in program,
such as variables, constants, and functions.
Some identifiers are predefined; others are defined by the user. All identifiers
must obey C++'s rules for identifiers: they may consist of letter, digits,
and the underscore character (_), and must begin with a letter or underscore
(though one is cautioned against beginning a indentifiers with underscores
as compatibility issues could arise between the object program abd the system
resources provided by the SDK); no other symbols are permitted to form an
identifier. Note that C++ is case sensitive.
Two predefined identifiers encountered frequently are cout,
which is used when generating output, and cin,
which is used to input data. Unlike reserved words (keywords), predefined
identifiers can be redefined, though one is cautioned not to do so.
Data type refers to a set of values together with a set of operations. C++ data types fall into the following three categories: simple data type, structured data type, and pointers.
A data type is called simple if the variable or named constant of that type can store only one value at a time. For example, if x is an int variable, at a given time only one value can be stored in x. The implication of this explanation will become clearer as we progess through these pages.
There are three categories of simple data:
The Integral type is further divided into nine categories: char, short, int, long, bool, unsigned char, unsigned short, unsigned int, and unsigned long. For our purposes, we will focus on the int, bool, and char data types.
C++ provides the floating-point data type to deal with decimal numbers. To represent real numbers, C++ uses a form of scientific notation called floating-point notation. The real numbers 75.924 and 0.18 might be expressed in C++'s floating-point notation as 7.592400E1 and 1.800000E-1 respectively, (where the letter E stands for the exponent).
C++ provides three data types to manipulate decimal numbers: float, double, and long double.
string Type
The string data type is a programmer-defined data type. Unlike the previously discussed data types however, the string data type is not available for use in a program. To use this data type, one must access program components from the library.
A string is a sequence of zero or more characters. Strings in C++ are enclosed in double quotation marks. A string containing no characters is called a null or empty string.
Every character in a string has a relative postion in the string. The position of the first character is 0, the position of the second character is 1, and so on. The lenght of the string is the number of characters (including spaces) in it.
There are five arithmetic operators:
The +, -, *, and / operators can be used with both integral and floating-point data types. The % is used only with the integral data type to find the remainder in ordinary division. Using the / with the integral data type returns the quotient in ordinary division. That is, integral division truncates any fractional part; there is no rounding.
Formally, an arithmetic expression is constructed using arithmetic operators and numbers. The numbers appearing in the expression, that are used to evaluate an operator are are called the operands for that operator.
Higher level: *, /, %
Lower level: +, -
When operators have the same level of precedence, the operations are performed from left to right. Furthermore, operations grouped in parenthesis have the highest priority.
Note: Because the char data type is also an integral data type, one could perform arithmetic operations on char data. However, one is advised to do this with caution as there is a difference between the character '8' for instance, and the integer 8. The integer value of 8 is 8; the integer value of '8' is 56, which is the ASCII collating sequence for the character '8'.
If all operands (i.e. numbers) in an expression are integer, the expression is called an integral expression. If all operands in an expression are floating-point numbers, the expression is called a floating-point or decimal expression. An integral expression yields an integral result; a floating-point expression yields a floating-point result.
An expression that has operands of different data types is called a mixed expression. Two rules apply when evaluating mixed expressions:
When a value of one data is automatically changed to another data type (as is the case when an integer is converted to a floating-point when an operator has both types of operands), an implicit type coercion is said to have occured. If one is not careful about data types, implicit type coercions can generate unexpected results.
To avoid implicit type coercions, C++ provides for explicit type conversion through the use of a cast operator. The cast operator, also called type conversion or type casting, takes the form:
static_cast<dataTypeName>(expression)
For example:
static_cast<int>(7.9)
static_cast<double>(25)
static_cast<double>(15)/2
First, the expression is evaluated. Its value is then converted to a value of the type specified by dataTypeName. The examples evaluate to 7, 25.0, and 7.5 respectively. Note that the type casting applied only to the expression within the parenthesis. static_cast is a reserved word in C++.
Note: One could also use cast operators to explicitly convert char data values into int data values, and int data values into char data values. Again, C++ uses the collating sequence for these conversions. The following lines of code will return 65 and 'A' respectively.
static_cast<int>('A')
static_cast<char>(65)
Recall that data must be loaded into main memory before it can be manipulated. Storing data in the computer's memory is a two-step process:
When one instructs the computer to allocate memory, one tells it not only what names to use for each memory location, but also what type of data to store in those memory locations. Some types of data need to be protected from accidental change during program execution. In C++, we use a named constant to instruct a program to mark those memory locations in which data is fixed thoughout program execution. A named constant, then, is a memory location whose content is not allowed to change thoughtout program execution. The syntax to declare a name constant is:
const dataType identifier = value;
For example:
const double conversion = 2.54;
const int noOfStudents = 20;
const char blank = ' ';
const double payRate = 15.57;
Memory cells whose contents can be modified during program execution are called variables. Formally, a variable is a memory location whose content may change during program execution. The syntax for declaring a variable or multiple variable is:
dataType identifier, identifier, ...;
For example:
double amountDue;
int counter;
char ch;
int x, y;
string name;
Note: In C++, the term variable, technically refers to a variable memory location. One must declare all identifiers before one can use them.
In C++, we can place data into a variable in two ways:
The assignment statement takes the form:
variable = expression
In an assignment statement, the value of the expression should match the data type of the variable. The expression on the right is evaluated, and its value is assigned to the variable (and thus to a memory location) on the left side.
A variable is said to be initialized the first time a value is placed in the variable.
In C++, = is called the assignment operator.
When a variable is declared, C++ does not automatically put a meaningful value in it. That is, C++ does not automatically initialize variables. For example, the int and double data type variables are not initialized to 0 as happens in some programming languages. When a variable is declared, memory is allocated for it.
However, if one only declares a variable and does not initialize it (i.e. ask the computer to put data into the variable), the value of that (memory) variable is garbage (usually the value of the setting of the bits from the variable's last use--and there is no way to know what that value is).
To avoid the complications that could arise from this situation enumerated above, C++ allows the programmer to initialize variable while they are being declared.
Consider the following lines of code ...
int first,
second;
char ch;
double x, y;
first = 13;
second = 10;
ch = ' ';
x = 12.6;
y = 123.456;
... in which we could declare and initialize the variables at the same time, thus:
int first = 13, second = 10;
char ch = ' ';
double x = 12.6, y = 123.456;
Putting data into variables from the standard input device (usually the keyboard) is accomplished via the use of cin and the operator >>, in the form:
cin >> variable >> variable ... ;
In C++, >> is called the stream extraction operator.
The input statement works as follows: Suppose miles is a variable of the data type double. The statement:
cin >> miles;
cause the computer to get a value, from the standard input device, of the data type double, and places it in the memory cell names miles.
By using more than one variable with cin, more than one value can be read at a time. Suppose feet and inches are variables of the data type int. A statement such as:
cin >> feet >> inches;
gets two integers from the keyboard and places them in the memory locations feet and inches, respectively.
The statement cin >> feet >> inches; is equivalent to the following statements:
cin >> feet;
cin >> inches;
The increment operator (++) increases the value of a variable by 1, and the decrement operator (--) decreases the value of a variable by 1. Increment and decrement variable have two forms, pre and post:
Pre-increment: ++variable
Post-increment: variable++
Pre-decrement: --variable
Post-decrement: variable--
If the pre-increment or pre-decrement operators are used in an expression, the value of the variable is first incremented or decremented by 1, and then the new value of the variable is used to evaluate the expression. On the other hand, if the post-increment or post-decrement operators are used in an expression, the current value of the variable is first used in the expression, and then the value of the variable is incremented or decremented by 1.
In C++, output on the standard output device (usually the screen) is accomplished via the use of the use of cout and the operator <<. The syntax of cout together with << is:
cout << expression or manipulator << expression or manipulator ... ;
This is called the output statement; in C++, << is called the stream insertion operator.
Generating output with cout follows two rules:
Consider the following lines of code:
cout << "Hello there!" << endl;
cout << "34 + 56 = " << 34 + 56 << endl;
cout << 'A' << endl;
cout << "Hello \nthere!." endl;
The first three lines return Hello there!, 90, and A respectively. The fourth line however returns Hello on one line, and there! on the next. \n is the newline character; it causes the insertion point to move to the beginning of the next line before printing there. In C++, \ is called the escape character and \n is called the newline escape sequence.
Note: The difference between the newline escape sequence (\n) and the endl manipulator is that \n may be placed within a string to move the insertion point to the next line. endl may only be used at the end of a line statement, to signify the end of that line and move the insertion point to the next line.
Also note that the output of the statment:
cout << '\n';
is the same as the output of the statement:
cout << "\n";
which is equivalent to the output of the statement:
cout << endl;
If one wished to output the sentences, "The quick brown fox jumped over the lazy dog. The cow leapt over the moon," one might find that the sentence does not fit in one line as part of the output statement. One could use multiple statements as follows:
cout << "The quick brown fox jumped over the lazy
dog. ";
cout << "The cow leapt over the moon." << endl;
Note the semicolon at the end of the first statement and the identifier cout at the beginning of the second statement. Note, too, that there is no endl manipulator at the end of the first statement.
Equivalently, one could use the following output statement to output the sentence:
cout << "The quick brown fox jumped over the lazy
dog. "
<< "The cow leapt over the moon."
<< endl;
Note that there is no semicolon at the end of the first line and the identifier cout does not appear at the beginning of the second line. Because there is no semicolon at the end of the first line, the output statement continues at the second line. Note that the long string is basically broken into two shorter strings, each within its own set of double quotation marks, both still parts of the same output statement.
The statement:
cout << "The quick brown fox jumped over the lazy
dog.
The cow leapt over the moon."
<< endl;
would be illegal in C++ because the return (or Enter) key on the keyboard cannot be part of a string. In effect, a string cannot be broken into more than one line by using the return (Enter) key on the keyboard.
We've discussed the newline escape sequence (\n). It will be useful to know some of the commonly used escape sequences at this juncture.
| Escape Sequence | Description | |
|---|---|---|
| \n | Newline | Insertion point moves to the beginning of the next line |
| \t | Tab | Insertion point moves to the next tab stop |
| \b | Backspace | Insertion point moves one space to the left |
| \r | Return | Insertion point moves to the beginning of the current line (not the next line) |
| \\ | Backslash | Backslash is printed |
| \' | Single quotation | Single quotation is printed |
| \" | Double quotation | Double quotation is printed |
Only a small number of operations such as arithmetic and assignment operations are explicitly defined in C++. Many of the functions and symbols needed to run a C++ program are provided as a collection of libraries. Every library has a name and is referred to by a header file. We use preprocessor directives and the name of header files to tell the computer the location of the code provided in the libraries. Preprocessor directives are processed by a program called a preprocessor. All preprocessor commands begin with #. There are no semicolons at the end of preprocessor commands because they are not C++ statements. To use a header file in a C++ program, use the preprocessor directive include:
#include <headerFileName>
For example:
#include <iostream>
Preprocessor directives (to include header files) are placed at the first line of the program so that the identifiers declares in those header files can be used throughout the program. (See Appendix F of D. S. Malik for a description of some of the commonly used header files. Note that individual programmers can also create their own header files).
A C++ program goea through six steps:
C++ Program
Editor [1]
Preprocessor
[2]
Compiler +
Library [3]
Linker
[4]
Loader [5]
Execution [6]
We can conclude that a C++ system has three basic components:
Recall that both cin and cout are predefined identifiers, declared in the header file iostream within the namespace std. There are several ways to use an identifier declared in the namespace std. One is to refer to cin and cout as std::cin and std::cout throughout the program; another is to include the statement
using namespace std;
after the statement #include <iostream>.
Note: Other header files include cmath (which contains the specifications of many mathematical functions), the iomanip (which contains the specifications of many functions and manipulators that help to format output in a specific manner.
Recall that the string data type is a programmer-defined data type and is not directly available for use in a program. To use the string data type, one must access its definition from the header file string. Thus, to use the string data type, one must include the following preprocessor directive:
#include <string>
at the top of the program.
The statements to declare variables, manipulate data (such as assignments), and input and output data are placed within the function main. The statements to declare named constants however are usually placed outside of the function main. The syntax of the function main has the form:
int main()
{
statement1
...
statement n
return 0;
}
Each statement (statement1,...statementn) is usually either a declarative statement or an executable statement. The statement return 0; must be included in the function main, and must be the last statement.
Declaration Statements are used to declare things such as variables
int a, b, c;
double x, y;
Executable statements perform calculations, manipulate data, create output, accept input, et cetera.
a = 4; //assignment statement
cin >> b; //input statement
cout << a << " " << b << endl; //output
statement
*****see portion that enumerates the program development progression for source code (*.cpp), to objects (*.obj), to executables (*.exe), p. 67
In a skeletal form, a C++ program looks like the following:
| preprocessor directive to include header files | #include <iostream> |
| using statement | using namespace std; |
| declare named constants | constant int number = 12; |
| int main() | |
| { | |
| statement1 | |
| ... | |
| statement n | |
| return 0; | |
| } |
Consider the following aspects of program style and form, and be sure to understand the ramifications of each.
Page Last Updated: Saturday February 12, 2005 10:21 AM