Professional Documents
Culture Documents
ch2 Lec1-1
ch2 Lec1-1
Example
Task: add two numbers
Pseudocode:
Start
Get two numbers
Add them
Print the answer
End
Flowchart
A flowchart is a graphical diagram that depicts the “flow” of a program using a
series of standard geometric symbols and lines, which are connected according
to the logic of the algorithm. There are a lot of symbols used in flowcharting
but the common five are:
Programming control structures
Programming control structure controls how the each
statement (step) of the algorithm flows (executes). Control
structures in most programming languages typically include
the following.
1. Sequence
The sequence control structure is the straightforward
execution of one processing step after another.
C++ is a
general –purpose
case sensitive
free form programming language that support procedural
object oriented and
generic programming.
C++ is a vendor-neutral (C++ standard is the same in any compiler or
platform).
The first C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!\n";
return 0;
}
All (non-white-space) characters are important
C++ is case sensitive: return ≠ Return
Any C++ program file should be saved with file name extension “.CPP ”
Parts of a Simple Program
#include – preprocessor directive
includes the contents of another file
using namespace – where to search unknown names
main() – function, starting point of the program
When the program starts, main() is called automatically.
Every C++ program has a main() function.
The end of a single statement ends with semicolon (;)
Comments:
// comment which ends at the end of the line
/* comment which may have multiple lines. It ends here: */
Elements of a Program
In C++, we can declare variables any where in the source code. But we should
declare a variable before using it no matter where it is written
Variables may be classified as global or local.
A global variable is one that can be shared by all parts of a program, including any
functions or sub-programs as long as it is declared first. A variable declared before
any function immediately after the include statements are global variables.
A local variable is one that is used only within a certain part of the program, for
example, only in one function or sub-program.
In the following example, the integer data type num1 is accessible
everywhere whereas z and is only accessible in the add function and
num2 is accessible in main function. This means cout<<z; or any
statement involving z is only valid in add function
Characters.
Characters variables (type char) are typically one byte in size, enough to hold
256 different values. A char can be represented as a small number (0 - 255).
Char in C++ are represented as any value inside a single quote.
E.g.: ‘x’, ‘A’, ‘5’, ‘a’, etc.
When the compiler finds such values (characters), it translates back the value to
the ASCII values. E.g. ‘a’ has a value 97 in ASCII.
Special Printing characters.
In C++, there are some special characters used for formatting.
These are:
\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\' single quote (')
\" double quote (")
\? question mark (?)
\\ backslash (\)
Operators.
57
A Simple C++ Program
• <iostream>
• Must be included for any program that outputs data to the screen or inputs
data from the keyboard using C++ style stream input/output.
• Replaces <stdio.h> of C
• C++ requires you to specify the return type, possibly void, for all
functions.
• Specifying a parameter list with empty parentheses is equivalent to
specifying a void parameter list in C.
58
Notes on Simple C++ Program
59
Header Files
• C++ Standard Library header files
• Each contains a portion of the Standard Library.
• Function prototypes for the related functions
• Definitions of various class types and functions
• Constants needed by those functions
• “Instruct” the compiler on how to interface with library and user-written components.
• Header file names ending in .h
• Are “old-style” header files
• Superseded by the C++ Standard Library header files
• Use #include directive to include class in a program.
60
C++ Standard Library header files
C++ Standard
Library header Explanation
files
<iostream> Contains function prototypes for the C++ standard input and
standard output functions. This header file replaces header file
<iostream.h>. This header is discussed in detail in
Chapter 26, Stream Input/Output.
<iomanip> Contains function prototypes for stream manipulators that format
streams of data. This header file replaces header file
<iomanip.h>. This header is used in Chapter 26, Stream
Input/Output.
<cmath> Contains function prototypes for math library functions. This
header file replaces header file <math.h>.
<cstdlib> Contains function prototypes for conversions of numbers to text,
text to numbers, memory allocation, random numbers and various
other utility functions. This header file replaces header file
<stdlib>.
<ctime> Contains function prototypes and types for manipulating the time
and date. This header file replaces header file <time.h>.
<vector>, These header files contain classes that implement the C++
<list> Standard Library containers. Containers store data during a
<deque>, program’s execution.
<queue>,
<stack>,
<map>,
<set>,
<bitset>
61