Programming Fundamentals
Program Structure
2019-11-10
by
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
SHEHZAD LATIF
Assistant Professor,
Hajvery University – Lahore
Email: Shehzadch49@yahoo.Com
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++
The most important elements added to C to create
C++ concern classes, objects, and object-oriented
programming.
• C++ was originally called “C with classes.”
2
Basics of a typical
C++ Environment
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
• The first phase consist of editing a file.
2019-11-10
• The programmer types a C++ program with editor.
• The program source file is then stored on secondary storage device
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
such as disk.
• C++ program file names often end with .cpp
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.
• Preprocessor obeys commands called preprocessor directives.
• Indicates that certain manipulations are to be performed on the program
before compilation.
6
Linker
• The preprocessor is invoked by the compiler before the program
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
functions to produce an executable image.
• If the program compiles and links correctly, an executable image
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
executable image from disk and transfers it to memory.
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
computer can run as it does other programs.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• Source files are text files (extension .CPP)
• Executable files have the .EXE extension
10
Your First Program
Programming Fundamentals by
2019-11-10
11
SHEHZAD LATIF (03134797617)
Preprocessor Directives
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.
• A preprocessor directive, on the other hand, is an instruction to
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
the compiler.
• A part of the compiler called the preprocessor deals with these
directives before it begins the real compilation process.
13
Program Statement vs
Preprocessor Directive
2019-11-10
• The preprocessor directive #include tells the
compiler to insert another file into your source
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
file.
• The type file usually included by #include is
called a header 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
• iostream is an example of a header file.
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
being used incorrectly
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().
• If there is no function called main() in your program, an error will be reported
when you run the program.
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
function has a return value of type int.
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.
• In this example there are only three statements in the function
body:
• the line starting with cout, the line starting with getch and the
line starting with return.
• However, a function body can consist of many statements.
19
Program Statements
• There are three statements in the FIRST program: the line
2019-11-10
• cout << “Hello World: My first C++ program”;
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• getch();
• and the return statement return 0;
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.
• It directs the contents on its right to the object on its left.
• In our example it directs the string constant “Hello World: My first C++
program” to cout, which sends it to the display.
21
String Constants
• The phrase in quotation marks, “Hello World: My first C++ program”, is
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
throughout the program’s existence.
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
• getch() apart from holding the screen or waiting for a character to be
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
• Compiler ignores whitespace almost completely.
2019-11-10
• Whitespace is defined as spaces, tabs and newlines.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
• These characters are invisible to the compiler.
• You can put several statements on one line, separated by any
number of spaces or tabs, or you can run a statement over two or
more lines. 25
Two new lines
Programming Fundamentals by
2019-11-10
26
SHEHZAD LATIF (03134797617)
Exceptions to the rule
• The first line of the program, starting with #include, is a preprocessor directive,
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
SHEHZAD LATIF (03134797617)
Comments
• 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
SHEHZAD LATIF (03134797617)
2019-11-10
• If you attempt to use the // style comment in this case, the closing
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
the code won’t compile correctly.
32
QUESTIONS ??
Programming Fundamentals by
2019-11-10
33
SHEHZAD LATIF (03134797617)
Outputs
Programming Fundamentals by
2019-11-10
34
SHEHZAD LATIF (03134797617)
Programming Fundamentals by
2019-11-10
35
SHEHZAD LATIF (03134797617)