Programming Fundamental
Lecture #2: By Dr. Mumtaz Ali, CUSIT
1
Computer Programming
Computer is a powerful tool.
It is not intelligent!
In order to use computer to solve our problems, we must tell it what we
want to be done and the order in which we want it be done.
These instructions are called computer program.
This process is called computer programming.
The person giving these instructions is called a computer programmer.
2
Program
An organized list of instructions that a computer can
understand to solve a problem or produce a desired result.
A set of instructions telling a computer what to do.
When program is executed, it causes the computer to behave
in a predetermined manner.
Without programs, computers are useless.
A program is like a recipe.
It contains a list of ingredients (called variables) and a list of
directions (called statements) that tell the computer what to do
with the variables.
The variables can represent numeric data, text, or graphical
images.
3
Software /Application program
Software /Application program is a collection
of instructions that enable the user to interact with a
computer, its hardware, or perform tasks.
For example, MS-office, Photoshop etc.
4
System program
Those programs that make the hardware availability
possible and manage system utilities e.g.
there are always lots of programs running on the
background of an operating system, but since they weren't
developed for the end-user, they are not applications.
Example memory management,
5
1 // Fig. 1.2: fig01_02.cpp
2 // A first program in C++
Comments
3 #include <iostream> Written between /* and */ or following a //.
4 Improve program readability and do not cause the
5 int main() computer to perform any action.
6 {
preprocessor directive
7 std::cout << "Welcome to C++!\n";
Message to the C++ preprocessor.
8
Lines beginning with # are preprocessor directives.
9 return 0; // indicate that program ended successfully
#include <iostream> tells the preprocessor to
10 } include
C++ the contents
programs containofone
theor
filemore
<iostream>, which
functions, one of
includes input/output
which must be main operations (such as printing to
the screen).
Parenthesis are used to indicate a function
int means
Prints the string of characters that main
contained "returns"
between the an integer value.
quotation marks. More in Chapter 3.
return is a way to exit a function
from a function. A left brace { begins
The entire line, including std::cout, the the
<< body of every function
return 0, in this case, operator, and a right to
means thatthe string "Welcome braceC++!\n"
} ends it. and
the program terminated normally.
the semicolon (;), is called a statement.
All statements must end with a semicolon.
6
Preprocessor directives
Preprocessor directives are lines included in the code of
programs preceded by a hash sign (#).
These lines are not program statements but directives for
the preprocessor.
The preprocessors are the directives, which give
instructions to the compiler to preprocess the information
before actual compilation starts.
The preprocessor examines the code before actual
compilation of code begins and resolves all these directives
before any code is actually generated by regular
statements.
7
Preprocessor directives
Preprocessor directives are not C++ statements, so they do
not end in a semicolon (;).
You already have seen a #include directive in the example.
This macro is used to include a header file into the source
file.
There are number of preprocessor directives supported by
C++ like #include, #define, #if, #else, #line etc.
8
Header Files
Header files contain definitions of Functions and Variables,
which is imported or used into any C++ program by using the
pre-processor #include statement.
Header file have an extension ".h" which contains C++
function declaration and macro definition.
Each header file contains information (or declarations) for a
particular group of functions.
Like conio header file contains declarations of standard input
and output functions available in C++ which is used for get the
input and print the output.
Similarly, the header file math.h contains declarations of
mathematical functions available in C++.
9
Header Files
10
Header Files
When we want to use any function in our C++ program
then first we need to import their definition from C++
library, for importing their declaration and definition we
need to include header file in program by using #include.
Header file include at the top of any C++ program.
For example if we use clrscr() in C++ program, then we
need to include, conio.h header file, because in conio.h
header file definition of clrscr() (for clear screen) is written
in conio.h header file.
11
Namespace
A namespace is used as additional information to
differentiate similar functions, classes, variables etc. with
the same name available in different libraries. Using
namespace, you can define the context in which names are
defined. In essence, a namespace defines a scope.
A namespace definition begins with the
keyword namespace followed by the namespace name as
follows.
namespace namespace_name
{ // code declarations }
12
Program is created in the
Editor Disk editor and stored on disk.
Preprocessor program
Preprocesso Disk processes the code.
r
Compiler creates
Compiler Disk object code and
stores it on disk.
Linker Disk Linker links the
object
Primary Memorycode with the
Loader libraries
Loader puts
program in memory.
Disk ..
..
..
Primary Memory CPU takes each
CPU instruction and
executes it, possibly
storing new data
..
..
values as the
..
program executes.
13
END
14