Programming Fundamentals (PF)
2nd Week (Lab #1 & 2)
Mr. Niamat Ullah Junior lecturer
Department of CS & IT,
Sarhad University, Peshawar.
Basic Structure of C++ Program
The format of writing program in C++ is called it structure.
It consists of the following parts:
Preprocessor directive
Namesaces (using namesace std)
Main () Function
Program body (C++ statements)
Preprocessor directive:
Preprocessor directive is an instruction given to the
compiler before the execution of actual program.
Preprocessor directive is also known as compiler
directive.
The Preprocessor directives are processed by a program
known as preprocessor.It is part of C++ compiler.
The Preprocessor directives start with hash symbol #.
These directives are written at the start of program.
The semicolon is not used at the end of Preprocessor
directives.
Include Preprocessor directive is used to include header
files in the program.
Header Files:
Header Files are collection of standard library functions
to perform different tasks.
There are many header files for different purposes.
Each header files contains different types of predefined
functions.
Many header files can be included in one program. The
header file must be included in the program before
calling any of its functions in the program.
The name of header file is written in angle brackets.
#include <header_file_name>
Or
#include “header_file_name”
Namespace:
A namespace is a declarative region that provides a scope to
the identifiers (the names of types, functions, variables,
etc.) inside it.
or
A namespace is a container that holds a set of identifiers,
such as functions, variables, classes, or other objects, and
provides a way to organize and encapsulate them.
They help prevent naming collisions, where two different
parts of code use the same identifier.
Main function:
The main () function is the starting point of a C++
program. When the program is run, the control enters
main () function and starts executing it statements.
Each program must contain main () function. If a
program does not contain main function, it can be
compiled but cannot be executed.
C++ statements:
A statement in C++ language is an instruction for the
computer to perform a task. Computer perform these
instructions one by one in the same sequence in which
these instructions are written.
The statements of the program are written in curly
braces ( {} ).The braces are also known as delimiters.
Each statement in C++ language is terminated by
semicolon (;).
Using cout:
The cout object is used to print a message on the
screen.
The message may consist of strings and values. A string
is a set of characters.
The cout object is used with the insertion operator
“<<”. Anything written after the insertion operator is
displayed on the screen.
Program example #1: Write a Program in C++ to display “Hello
World” on Screen / console using cout.
#include<iostream>
using namespace std;
int main()
{
cout<< “Hello World”;
return 0;
}
Program example #2: Write a program to display a first character
of your name using cout.
#include<iostream>
using namespace std;
int main()
{
cout<< ‘N’;
return 0;
}
Program example #3: Write a program that display the add of
30+50 using cout.
#include<iostream>
using namespace std;
int main()
{
cout<< 30+50;
return 0;
}
Lab TASKs
Lab Task #2: Write a Program in C++ to display your name on
Screen using cout.
Lab Task #3: Write a C++ program to print the following lines:
“Welcome to C++ programming”.