Programming Fundamentals: Program Structure
Programming Fundamentals: Program Structure
Program Structure
2019-11-10
by
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
SHEHZAD LATIF
Assistant Professor,
Hajvery University – Lahore
Email: [email protected]
1
C++ & C
C++ is derived from the C language.
It is a superset of C
2019-11-10
• Almost every correct statement in C is also a
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
correct statement in C++
programming.
2019-11-10
It consist of 6 phases
• Editor
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• Preprocessor
• Compilers
• Linkers
• Loaders
3
• Execute
Program is created in the
Editor Disk editor and stored on disk.
Preprocessor program
Preprocessor Disk processes the code.
Compiler creates
Compiler Disk object code and stores
2019-11-10
it on disk.
Linker Disk Linker links the object
code with the libraries
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
Primary Memory
Loader
Loader puts program
in memory.
Disk ..
..
..
Primary Memory
CPU takes each
CPU instruction and
executes it, possibly
storing new data
..
.. values as the 4
..
program executes.
Edit
2019-11-10
• The programmer types a C++ program with editor.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
such as disk.
5
Compile
• Next, the programmer gives the command to compile the program.
2019-11-10
• The compiler translates the C++ program into machine language code.
• In C++ system
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• A preprocessor program executes automatically before the compiler’s translation
phase begins.
before compilation.
6
Linker
2019-11-10
is converted to machine language.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• The next phase is called linking.
• A linker links the object code with the code for the missing
is produced.
7
Loader
• The next phase is called loading.
2019-11-10
• Before a program can be executed, the program must first be
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
placed in memory. This is done by the loader, which takes the
8
Execute
• Finally, the computer executes the program one instruction at a time.
2019-11-10
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
9
Basic Program
Construction
2019-11-10
• Compilers take source code and transform it into executable files, which your
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• Source files are text files (extension .CPP)
10
Your First Program
Programming Fundamentals by
2019-11-10
11
The first two lines that begin the program are preprocessor directives.
2019-11-10
e.g., #include <iostream.h>
#include <conio.h>
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
It isn’t a program statement
It isn’t a part of a function body
It does not end with a semicolon
12
Program Statement vs
Preprocessor Directive
• Program statements are instructions to the computer to do
2019-11-10
something, such as adding two numbers or printing a sentence.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
the compiler.
13
Program Statement vs
Preprocessor Directive
2019-11-10
• The preprocessor directive #include tells the
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
file.
14
Header Files
• The preprocessor directive #include tells the compiler to add the file
2019-11-10
iostream to the source file before compiling.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
Why do this?
15
Header Files
2019-11-10
• It’s concerned with basic input/output operations, and contains declarations that
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
are needed by the cout identifier and the << operator.
• Without these declarations, the compiler won’t recognize cout and will think << is
16
Always Start with main()
• When you run a C++ program, the first statement executed will be at the
2019-11-10
beginning of a function called main().
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• The program may consist of many functions, but on startup, control always goes
to main().
17
Functions
• The parentheses following the word main are the distinguishing feature of a
2019-11-10
function.
• Without parentheses compiler would think that main refers to some other
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
program element.
• The word int preceding the function name indicates that this particular
18
Braces and the Function Body
• The body of a function is surrounded by braces (sometimes
2019-11-10
called curly brackets).
• Every function must use this pair of braces around the function
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
body.
• the line starting with cout, the line starting with getch and the
line starting with return.
2019-11-10
• cout << “Hello World: My first C++ program”;
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• getch();
20
Output Using cout
• The identifier cout (pronounced “C out”) is actually an object. It is
2019-11-10
predefined in C++ to correspond to the standard output stream.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• The operator << is called the insertion or put to operator.
• In our example it directs the string constant “Hello World: My first C++
2019-11-10
an example of a string constant.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• Its value is set when the program is written, and it retains this value
22
Program Statements
• The first statement tells the computer to display the quoted phrase.
2019-11-10
• A semicolon signals the end of the statement.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• If you leave out the semicolon, the compiler will signal an error.
23
Program Statements
2019-11-10
entered to exit the output screen , it also print the input given by the user.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• The last statement in the function body is return 0;
• This tells main() to return the value 0 to whoever called it, in this case the
operating system or compiler. The value 0 indicates that the program had
terminated successfully.
24
Whitespace
2019-11-10
• Whitespace is defined as spaces, tabs and newlines.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• These characters are invisible to the compiler.
more lines. 25
Two new lines
Programming Fundamentals by
2019-11-10
26
2019-11-10
which must be written on one line.
• Also, string constants, such as “Every age has a language of its own”, cannot
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
be broken into separate lines.
• If you need a long string constant, you can insert a backslash(\) at the line
break or divide the string into two separate strings, each surrounded by
quotes.
27
Backslash
Programming Fundamentals by
2019-11-10
28
• They help the person writing a program, and anyone else who must
2019-11-10
read the source file, understand what’s going on.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• The compiler ignores comments
29
Comment Syntax
• Comments start with a double slash symbol (//) and terminate at the
2019-11-10
end of the line.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
30
Alternative Comment Syntax
Programming Fundamentals by
2019-11-10
31
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
brace won’t be visible to
the compiler-- since a // style comment runs to the end of the line—and
32
QUESTIONS ??
Programming Fundamentals by
2019-11-10
33
Programming Fundamentals by
2019-11-10
34