Académique Documents
Professionnel Documents
Culture Documents
Programming in C++
Thinking in C++
by Bruce Eckel
EEN-103
Programming in C++
Basic Programming in C++: Concepts of algorithm & flow
charts; Input/output, constants, variables, expressions
and operators; Naming conventions and styles;
Conditions and selection statements; Looping and control
structures (while, for, do-while, break and continue); File
I/O, header files, string processing; Pre-processor
directives such as #include, #define, #ifdef, #ifndef;
Compiling and linking.
Structure of a program
Comments
Comments are parts of the source
code disregarded by the compiler.
They simply do nothing. Their purpose
is only to allow the programmer to
insert notes or descriptions embedded
within the source code.
#include <iostream>
Lines beginning with a hash sign (#) are directives
for the preprocessor
They are indications for the compiler's preprocessor
Directive #include <iostream> tells the
preprocessor to include the iostream file
Angle brackets <
> are required to indicate
that the word iostream (which stands for
input/output stream) is the name of a Standard C+
+ Library file
The expression <iostream> is called standard
header
#include <iostream> is a
preprocessor directive that
specifies the C++ compiler
where to find the definitions of
cout or cin in the namespace
std
preprocessor
The preprocessor performs preliminary
operations on C++ files before they
are passed to the compiler.
Preprocessor
can
be
used
to
conditionally compile code, insert files,
specify compile-time error messages,
and apply machine-specific rules to
sections of code.
int main()
This line corresponds to the beginning of the
definition of the main function.
The main function is the point by where all C++
programs start their execution, independently
of its location within the source code.
It does not matter whether there are other
functions with other names defined before or
after it - the instructions contained within this
i.e. main() function's definition will always be
the first ones to be executed in any C++
program.
All C++ programs have a main function.
int main()
Identifier main is the name of a function,
called the main function of the program
Every C++ program must have one and
only one main () function
The parentheses () indicate that it is
function
The keyword int is the name of the data
type integer
It is used to indicate the return type for
main() function
return 0;
When the program has finished
running, it can return an integer
value to the operating system to
signal some resulting status
return 0;
The return statement causes the main function
to finish
Return may be followed by a return code
A return code of 0 for the main function is
generally interpreted as the program worked as
expected without any errors during its execution
This is the most usual way to end a C++ console
program
int main ()
{ cout <<
"Hello World!";
cout <<
"I'm a C++ program";
return 0; }
Basics
Building blocks of a program in C++
constants
variables
operators
expressions
statements
functions
objects
a) Constants
const float pi = 3.14159;
const int codes = 10;
const char size = M;
b) Variables
int j; int m = 5, n;
double d = 3.42; char c;
Basics contd.
c) Expressions built with operators
1. pi * d
2. m + ln(n)
3. n == m
a Boolean expression
d) statements make use of expressions
1. sum = a + b; //assignment statement
2. area = pi * d * d / 4.0;
Simple
Integral
Program Structure
Directive
(compiler directives)
Function definition
.
A program in C++ is a collection of functions, out of
which one function must be main.
The execution starts at main, which may call other
functions.
int main()
{
Statement
.
}
Programming in C++
Compiler Dev C++, Developed by Bloodshed
Softwares
Integrated Development Environment (IDE)
consists of
(1) Editor to create source program
(2) Compiler to compile source to object
(3) Debugger to find the errors and correcting
them
(4) Linker to create the executable file
What is an algorithm?
An algorithm is a procedure (a finite set of welldefined instructions) for accomplishing some task
which, given an initial state, will terminate in a
defined end-state.
Algorithms often have steps that repeat (iterate)
or require decisions (such as logic or comparison).
What is a program?
A computer program is a sequence of instructions
(written in a programming language) that, when
C++ Compiler
Some common compilers include Borland C+
+, Microsoft C++, and GNU C++.
The most common is Dev-C++ around GNU's
G++ compiler.
Each of these compilers is slightly different.
Each one should support the ANSI/ISO
standard C++ functions
Dev-C++
Bloodshed Software
http://www.bloodshed.net/dev/devcpp.h
tml
Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0
MB)
with Mingw/GCC 3.4.2
Download from:
SourceForge
Dev-C++
New Source File
Compile
Run
Program Errors
Syntax errors
Violation of the grammar rules of the language
Discovered by the compiler
Error messages may not always show correct location of
errors
Run-time errors
Error conditions detected by the computer at runtime
Logic errors
Errors in the programs algorithm
Most difficult to diagnose
Computer does not recognize an error
Concept of Variable
Let us think that I ask you to retain the
number 5 in your mental memory, and
then I ask you to memorize also the
number 2 at the same time.
You have just stored two different values in
your memory. Now, if I ask you to add 1 to
the first number I said, you should be
retaining the numbers 6 (that is 5+1) and 2
in your memory.
Values that we could now for example
subtract and obtain 4 as result.