Lecture 2. Introduction to C++_1
Lecture 2. Introduction to C++_1
Department
Computer Programming
(MEng 1052)
Lecture 2
Introduction to C++
2
Origins of the C++ Language
C is a general-purpose language that can be used for writing
any sort of program, but its success and popularity are closely
tied to the UNIX operating system.
The C language is peculiar because it is a high-level language
with many of the features of a low-level language.
C is somewhere in between the two extremes of a very high-
level language and a low-level language, and therein lies both
its strengths and its weaknesses.
C is not as easy to understand as other languages; also, it does
not have as many automatic checks as some other high-level
languages.
3
Origins of the C++ Language
To overcome these and other shortcomings of C, Bjarne
Stroustrup of AT&T Bell Laboratories developed C++ in
the early 1980s.
Stroustrup designed C++ to be a better C. Most of C is a
subset of C++, and so most C programs are also C++
programs.
The reverse is not true; many C++ programs are definitely
not C programs.
4
A Sample C++ Program
In the next lecture we will explain in detail all the C++
features you need to write programs , but to give you a feel
for how a C++ program works, we will now give a brief
description of how a sample C++ program works.
5
6
A Sample C++ Program
The program begins with the following lines:
9
A Sample C++ Program
10
A Sample C++ Program
The \n at the end of the quoted string tells the computer to
start a new line after writing out the text.
The next program line starts with the word cin, so it is an
input statement.
Let’s look at that line:
cin >> number_of_pods;
This line may be read, ‘get number_of_pods from cin’ or
simply ‘input number_of_pods’.
If you think of the word cin as standing for the keyboard
(the input device), then the arrows say that input should be
sent from the keyboard to the variable number_of_pods.
11
A Sample C++ Program
Consider the next two program lines:
cout << "Enter the number of peas in a pod:\n";
cin >> peas_per_pod;
The line first sends a message to the screen asking for a
number.
When you type in a number at the keyboard and press the
Return key, that number becomes the value of the variable
peas_per_pod.
The next nonblank program line, shown below, does all the
computation that is done in this simple program:
total_peas = number_of_pods * peas_per_pod;
The asterisk symbol, *, is used for multiplication in C++. So
this statement says to multiply number_of_pods and
12 peas_per_pod.
A Sample C++ Program
The sample dialogue of the above program is shown below
13
PROGRAMMING TIP: Input and Output Syntax
If you think of cin as a name for the keyboard or input device
and think of cout as a name for the screen or the output
device, then it is easy to remember the direction of the arrows
>> and <<.
They point in the direction that data moves. For example,
consider the statement:
cin >> number_of_pods;
In the above statement, data moves from the keyboard to the
variable number_of_pods, and so the arrow points from cin to
the variable. On the other hand, consider the output statement:
cout << number_of_pods;
In this statement the data moves from the variable
number_of_pods to the screen, so the arrow points from the
14
variable number_of_pods to cout.
Layout of a Simple C++ Program
The general form of a simple C++ program is shown below
15
Layout of a Simple C++ Program
#include <iostream>
is called an include directive. It tells the compiler where to find
information about certain items that are used in your program.
In this case iostream is the name of a library that contains the
definitions of the routines that handle input from the keyboard
and output to the screen; iostream is a file that contains some
basic information about this library.
The linker program that we discussed earlier in this chapter
combines the object code for the library iostream and the object
code for the program you write.
Directives always begin with the symbol #.
Be certain that you do not have any extra space between the <
and the iostream file name or between the end of the file name
and the closing >.
16
Layout of a Simple C++ Program
Some compilers require that directives have no spaces
around the #; so it is always safest to place the # at the very
start of the line and not include any space between the #
and the word include.
using namespace std;
This line says that the names defined in iostream are to be
interpreted in the “standard way” (std is an abbreviation of
standard).
int main( )
{
These two lines simply say that the main function of the
program starts here
17
Layout of a Simple C++ Program
return 0;
This line is called a return statement and is considered to be
an executable statement because it tells the computer to do
something; specifically, it tells the computer to end the
program.
The number 0 has no intuitive significance to us yet, but must
be there; its meaning will become clear as you learn more
about C++.
Note that even though the return statement says to end the
program, you still must add a closing brace } at the end of the
main part of your program.
18
Compiling and Running a C++ Program
You write a C++ program using a text editor in the same way
that you write any other document such as a term paper, a
shopping list, or whatever.
The program is kept in a file just like any other document you
prepare using a text editor.
When you give the command to compile your program, this will
produce a machine-language translation of your C++ program.
This translated version of your program is called the object code
for your program.
The object code for your program must be linked (that is,
combined) with the object code for routines (such as input and
output routines) that are already written for you. It is likely that
this linking will be done automatically, so you do not need to
19
worry about linking.
SELF-TEST EXERCISES
1. If the following statement were used in a C++ program,
what would it cause to be written on the screen?
cout << "C++ is easy to understand.";
c++ is easy to understand
2. What is the meaning of \n as used in the following
statement?
cout << "Enter the number of peas in a pod:\n";
begin in anew line or end
3. What is the meaning of this directive?
#include <iostream>
4. What, if anything, is wrong with the following #include
directives?
a) #include <iostream > gap at the end
b) #include < iostream> gap b/n < & iostream
c) #include <iostream> correct
20
TESTING AND DEBUGGING
A mistake in a program is usually called a bug, and the process
of eliminating bugs is called debugging.
Kinds of Program Errors
Syntax errors: are due to violation of the syntax (that is, the
grammar rules) of the programming language, such as omitting
a semicolon.
Run-time errors. Many run-time errors have to do with
numeric calculations. For example, if the computer attempts to
divide a number by zero, that is normally a run-time error.
Logic errors. Mistakes in the underlying algorithm or in
translating the algorithm into the C++ language. For example, if
you were to mistakenly use the addition sign + instead of the
multiplication sign * The program would compile and run
normally, but would give the wrong answer.
21
TESTING AND DEBUGGING
The computer will usually tell you about errors in the first
two categories. You must discover logic errors yourself.
22
Exercises
23
End of Lecture 2
Next Lecture
Lecture 3: C++ Basics
24