The following shows a simple C++ program which adds two integers. Click here to see an equivalent Pascal program.
Note that C++ is case-sensitive. For example, SUM, sum and Sum are considered different in C++.
If you are interested, you may also click here to see how C handles input and output (also supported by C++).
#include <iostream.h> |
preprocessor directive(s) |
main() { // A simple C++ program int x, y, sum; cout << "A program which adds two integers\n"; cout << "Enter 1st integer: "; cin >> x; cout << "Enter 2nd integer: "; cin >> y; sum = x + y; cout << "Sum is " << sum << endl; return 0; } |
program body |
Sample output (text in red is entered by user): | |
A program which adds two integers Enter 1st integer: 3 Enter 2nd integer: 4 Sum is 7 |
We will use this program to see the basic structure of a C++ program and I/O statements in C++.
Before compilation of a C++ program, it is processed by a preprocessor. The preprocessor directives tell the preprocessor to perform some manipulations which must be done before compilation. Almost all C++ programs has at least one preprocessor directive. A preprocessor directive begins with a #.
#include preprocessor directives tell the preprocessor to include the functions provided by the specified file. They are similar to the uses statements in Turbo Pascal.
For C++ programs that output data to the screen and/or input data from the keyboard, the following preprocessor directive must be present:
#include <iostream.h>
This preprocessor directive can be seen almost in every C++ program.
The program body consists of declarative and executable statements. In Program 1,
int x, y, sum;
is a declarative statement, while the other statements within the braces, {},
are executable statements.
Unlike Pascal, declarative statements need not come before executable statements in a C++ program. Sometimes a declarative statement may be found in the middle of the program body.
The line
main()
is a part of every C++ program. It indicates the beginning of the main program. In
addition, braces are required to enclose all the statements of the main program. The left
brace ({) and the right brace (}) are equivalent
to begin and end in Pascal.
In C++, each statement must be ended with a semicolon (;), even the last statement.
The last statement in the main program is
return 0;
A return statement is used in a function to exit the function and
return a value. Since the main program is considered as a function in C++, return
statement is also used. The main program returning 0 indicates successful termination.
In a C++ program, a line that begins with // indicates that the remainder of the line is a comment.
In addition, comments in C++ can also be enclosed by /* and */.
To print something on the screen, we can use a cout statement. It
starts with cout, then the << operator,
and finally the items to be displayed. For example, the statement
cout << "Enter the 1st integer: ";
instructs the computer to display the string "Enter the 1st integer: "
onto the screen. Here cout behaves like write in
Pascal.
In C++, a string of characters are enclosed by a pair of double quotation marks (").
cout can display multiple items (including strings, numbers and
values stored in variables). For example, the C++ statement
cout << "First value is " << a +
b << " and second value is " << c + d;
is equivalent to the Pascal statement
write('First value is ', a + b, ' and second value is
', c + d);
Unlike Pascal, there is no reserved word or standard identifier which behaves like writeln in Pascal. To output a newline character, we use \n
within the quotation marks. For example, the statement
cout << "A program which adds two
integers\n";
instructs the computer to display the following on the screen:
A program which adds two integers
The next character to be displayed will be on the next line.
Also, the statement "cout << "Welcome\nto C++!\n";" will causes the computer to display the following on the screen:
Welcome
to C++!
Note that \n inserts a newline character between "Welcome" and "to".
In C++, \ is an escape character. This means that a special character is to be output. The following table introduces some common escape sequences:
Escape sequence | Description |
\n | Newline character |
\t | Horizontal tab |
\\ | The backslash \ |
\" | The double quotation mark " |
For example, "cout << "1st\t2nd\t3rd\t4th\na\tb\tc\td\n";" instructs the computer to display the following on the screen:
1st 2nd 3rd 4th a b c d
endl is used with cout. It outputs a newline
and then forces any outputs accumulated in the buffer to be printed. For the
purpose of tracing simple programs, you may consider endl to be similar to the newline
character "\n".
For example (as seen in Program 1):
cout << "Sum is " << sum << endl;
To obtain a value from the keyboard and store it into a variable, we can use a cin statement. It starts with cin, then >> operator, and finally a variable.
In Program 1, the statement "cin >> x;" accepts an integer (x is an integer variable) from the keyboard and stores it in x.
If required, the cin statement can accepts multiple values and store them in the corresponding variables. For example, the statement "cin >> x >> y;" accepts two values from the keyboard and store them in x and y respectively (here x and y can be of different types).