CPP-Lecture-02 e
CPP-Lecture-02 e
CPP-Lecture-02 e
(Using C++)
Huma Israr
Department Of Information Engineering Technology
National Skills University, Islamabad.
huma.israr@nsu.edu.pk
Fall 2023, BS-CS
2
Week 02
Lecture – 01
Lecture – 02
Semester Calendar
3
Class Schedule
In C++ system
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.
Linking:
The preprocessor is invoked by the compiler before the
program is converted to machine language.
The next phase is called linking.
A linker links the object code with the code for the missing functions to
produce an executable image.
Getting Started
Building Program
&
Compiling
Structure of C++ Program
Source Code Output
return 0;
}
Components of C++ Program:
Line 1: // My First C++ Program
Lines Begin with two slash sign (//) are called comments by
the programmer and have no effect on the behavior of the
program .
Programmer use them to include short observation or
explanation concerning the code or program.
In this case, It is a brief introductory description of the
program.
Note: One line Comment
Components of C++ Program (Conti……)
Line 2: #include<iostream.h>
Lines Begin with hash sign (#) are called pre-processor
directives or pre-compiler directives, include some standard
library header files
In this case, the directive #include<iostream>, instruct the
pre-processor to include a section of standard c++ code ,
Known as header iostream , that allow standard input output
operations.
iostream makes input and output available to us
iostream standard header file that defines various types and
variables related to I/O
Components of C++ Program (Conti….)
Line 3: using namespace std;
legalizes" header contents” .
Use the standard namespace
Loads a namespace called std. Namespaces are used to
separate sections of code for programmer convenience. To
save typing we’ll always use this line in this course.
Components of C++ Program (conti….)
Line 4: int main()
int main() , begin function main()
The line initiate the declaration of the main function.
Essentially, a function is a group of instruction/statements
which are given name.
In this case, gives the name “ main “ to the group of
statements/instructions that follows.
The execution of all C++ programs begin with the main
function regardless of where the function is actually located
within the code.
Note: open and close braces { } mark the beginning and end of main function
Components of C++ Program (Conti…)
Line 5: cout<<“ ** Hello World ** ”;
Prints the message ** Hello World ** .
cout is an instance of iostream
cout is the object that writes to the stdout device, i.e. the
console window.
It is part of the C++ standard library.
Use with “using namespace std;”
Operator << is used for output
<< is the C++ insertion operator. It is used to pass characters
from the right to the object on the left.
Components of C++ Program:
Line 6: return 0;
The return statement returns an integer value to the
operating system after completion.
0 means “no error”. C++ programs must return an integer
value.