Chapter Two
Chapter Two
Basics of C++
Programming paradigm
Unstructured Programming
Procedural programming.
Structured Programming
disadvantages
Complex
if the same statement sequence is needed at
different locations within the program, the
sequence must be copied
Procedural programming
based upon the concept of procedure call
A procedure call is used to invoke the procedure
Procedures (routines, subroutines, methods,
functions) simply contain a series of
computational steps to be carried out to solve a
problem
We have a single program, which is divided into small
pieces called procedures
Advantage
to re-use the same code at different places in the
program without copying it
easier way to keep track of program flow
Example
FORTRAN, ADA
Structured Programming
is a subset of procedural programming (also
known as modular programming)
procedures of a common functionality are
grouped together into separate modules
Each module can have its own data
allows each module to manage an internal state
which is modified by calls to procedures of this
module
top-down design model
map out the overall program structure into
separate subsections
Advantage
Reuse
easier to understand and modify
Object Oriented Programming - OOP
First programmer
Ada Augusta, Countess of Lovelace
Colleague of Babbage
Programming and Problem Solving
Algorithm
A sequence of precise instructions which
leads to a solution
Program
An algorithm expressed in a language the computer
can understand
Program Design
Programming is a creative process
No complete set of rules for creating a program
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 run-time
Logic errors
Errors in the program’s algorithm
Most difficult to diagnose
Computer does not recognize such an error
Linking
C++ programs are typically created by
linking together one or more OBJ files
one or more libraries
A library is a collection of linkable files
A Linker combines
The object code for the programs we write
and
The object code for the pre-compiled routines
into
The machine language program the CPU can run
Summary
The steps to create an executable file are
Create a source code file, with a .CPP extension.
5. Load Loader
Loader puts program
6. Execute in memory.
Disk ..
..
..
Primary Memory
CPUtakes each
CPU instruction and
executes it, possibly
storing new data
..
.. values as the program
..
executes.
Showing Sample program
Every C++ program has a number of components
main function
some variable declarations
some executable statements
C++ Programs
#include <iostream>
executing
Keyboard program Screen
cin cout
SYNTAX
cin >> x;
cin >> y;
1. #include <iostream.h>
2. int main()
3. {
4. cout << "Hello World!\n";
5. return 0;
6. }
First Program in C++: Printing a Line of Text
(Cont.)
Preprocessor directives
Processed by preprocessor before compiling
Begin with #
Example
#include <iostream>
Tells preprocessor to include the input/output stream header file
<iostream>
Common Programming Error
return statement
One of several means to exit a function
When used at the end of main
The value 0 indicates the program terminated successfully
Example
return 0;
Basic Elements
Five kind of tokens in C++
Comments
Keywords (reserved words)
Identifiers
Literals
Operators
Comments
Remark about programs
Explain programs to other programmers
Improve program readability
Ignored by compiler
Two types
Single-line comment
Begin with //
Example
// This is a text-printing program.
Multi-line comment
Start with /*
End with */
Typical uses
Identify program and who wrote it
Record when program was written
Add descriptions of modifications
Example
1 /* Fig. 2.1: fig02_01.cpp
2 Text-printing program. */
3 #include <iostream> // allows program to output data to the screen
4
5 // function main begins program execution
6 int main()
7 {
8 cout << "Welcome to C++!\n"; // display message
9
10 return 0; // indicate that program ended successfully
11
Welcome to C++!
Every program should begin with a
comment that describes the purpose of
the program, author, date and time.
End of 2nd chapter
49