Lec 6 (How to Write a Program )Finish
Lec 6 (How to Write a Program )Finish
HOW TO WRITE A
PROGRAM
• Text editor
• The program is written using the text editor is known as source code.
• The source code (human readable instructions) is saved on to the secondary
storage section of the computer system (disk) with an extension ‘.Cpp’ to let the
compiler know that it is written in c++ language.
What is a preprocessor?
• Preprocessor: A program that modifies the source code by adding other files
and performing various text replacements
• It executes automatically before the translation period starts.
• Examples:
• #include <iostream> adds a file called a header file to the source code. It
contains, among other things, the prototypes for cin and cout functions.
• Compilation (step 3) follows immediately after the preprocessing, so, none can
access the modifications made by the preprocessor
What is a Complier?
Source Object
Code Compiler Code
.cpp .obj
What is a Linker?
• Linker: in this stage Linker links the more than one object files or libraries
and generates the executable file.
• scans the standard library, selects the needed function (precompiled) and upon
linking them into the object file, produces an executable file with extension ‘.Exe’
(for UNIX based system it is ‘.Out’) and stores it on to the disk.
• Libraries are a collection of functions/objects
• Example:
• The linker adds the precompiled function definitions for cin, cout, etc.
What is a Loader?
• Loader: This is a final stage of any C/C++ program execution process, in this
stage Loader loads the executable file into the main/primary memory. And
program run.
Example
Text Editor
/* hello.cpp */
#include <iostream>
int main() Preprocessor adds iostream text
{ (prototype for cout)
cout<<"hello world\n";
return 0;
Compiler converts to machine code
}
Linker
-add library
-add cout object code
-add object code for
other functions
hello.exe Executable
image
• Let us look at a simple code that would print the words hello world.
10
1 // Fig. 1.2: fig01_02.cpp
2
11//
11
A first program in C++
Comments
3 #include <iostream> Written between /* and */ or following a //
4
Improve program readability and do not cause the
computer to perform any action
5 int main()
6 {
preprocessor directive
7 std::cout << "Welcome to C++!\n"; Message to the C++ preprocessor
8
Lines beginning with # are preprocessor directives
#include <iostream> tells the preprocessor to
9 return 0; // indicate that program include
endedthe contents of the file <iostream>, which
successfully
10 }
includes input/output operations (such as printing to
C++ programs contain one or more functions, exactly
the screen).
one of which must be main
Welcome to C++! Parenthesis used to indicate a function
int means that main "returns" an integer value.
Prints the string of characters
More in Leccontained
functions.between the
return is one a way toquotation
exit a marks.
function. Left brace { begins the body of every function
return 0, in this case, The entire
means thatline, including std::cout, the << operator,
and a right brace } ends it
the program terminated the string "Welcome to C++!\n" and the
normally.
semicolon (;), is called a statement. 11
Identifier sum
Box 75 Built in type
// entry point
int main() main: single C++ statement !
{
float w_lb, w_kg; A declaration statement
// entry point
int main()
{
float w_lb, w_kg;
• Operators take one or more input values and produce one output
value
• E.G. + , - , * , / , < , > , <= , >= , && , ||
Operators
Binary Unary
Arithmetic Logical Bitwise Comparison Arithmetic
+ add && and & and < - less-than - negative
- sub || or | or > - gt.-than - ++ increment
* mul ^ xor <= - less-or-eq --decrement
/ div >= - gt-or-eq
% mod Copy == - equal
= Logical
!= - not-equal ! not
+=, -=, *=, /=, %=
Pointer * , &
UNARY EXPRESSIONS
int a=9, b;
• Pre-increment :
• b = ++a; b is 10 and a is 10
• Post-increment:
• b = a++; b is 9 and a is 10
• Pre-decrement :
• b = --a; b is 8 and a is 8
• Post-decrement:
• b = a--; b is 9 and a is 8
ARITHMETIC OPERATORS: + - * / %
• Simple copy: a = b;
• Overwrites the left object (l-value) with the result of the expression on the
right (r-value)
• L-value must be writable
• Copy with add
• A += b; is the same as a = a + b;
• Other copy-with-subtract, multiplication, division behaves the same
• A -= b; a *= b; a /= b; etc
COMPARISON OPERATORS
• < , > , =<, =>, ==, !=
• All yield boolean true (1) or false (0)
• 11<4 is false. 11<11 is false
• 11>4 is true. 11>11 is false
• 11>=11 and 11<=11 both are true
• 4==4 is true. 4!=4 is false
LOGICAL OPERATORS
• Use parenthesis
• Unary (++, --, !, -)
• Arithmetic (*, /, %, +, -)
• Comparison (< , <= , > , >=, ==, !=)
• Logical (&&, ||)
• Assignment (= , +=,-=,*= ,/= ,%=, etc)
EXPRESSIONS WITH SIDE EFFECTS
• R = x + y--; is equivalent to
• R = x + y; y = y-1; (two actions!)
• R = ++x - y; is equivalent to
• X = x+1; r = x - y; (two actions!)
32
3 #include <iostream>
5 int main()
1. Load
<iostream>
6 {
37
2.4 exit (return
0)
1 // Fig. 1.5: fig01_05.cpp
3 #include <iostream>
4 1. Load <iostream>
5 int main()
2. main
6 {
10 }
2.3 Print "to"
2.7 newline
38