You are on page 1of 33

Programming

Programming is like writing. If you can write a demonstration, you can make
a program.
But, you actually, programming is not so easy, because a real good program
is not easily programmed. It needs the programmers’ lots of wisdom, lots of
knowledge about programming and lots of experience.
It is like writing, to be a good writer needs lots of experience and lots of
knowledge about the world. Learning and practice are necessary

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 1


Programming Techniques and
Object-Oriented

Programming Techniques

The evolution of programming techniques is :

to make programming languages more


expressive

to develop complex systems more easily

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 2


Programming Techniques

Unstructured Programming structural Programming Procedural Programming modular Programming

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 3


Unstructured Programming

Usually, people start learning programming by writing small and


simple programs consisting only of one main program. Here ``main
program'' stands for a sequence of commands or statements which
modify data which is global throughout the whole program.

Main Program
Data

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 4


Drawbacks
This programming technique can only be used in a very small
program.

For example, if the same statement sequence is needed at


different locations within the program, the sequence must be
copied. If an error needed to be modified, every copy needs to
be modified.

This has lead to the idea to extract these sequences


(procedure), name them and offering a technique to call and
return from these procedures.

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 5


Procedural Programming
Main program

The procedural programming, you are able to Procedure


combine sequences of calling statements into
one single place.

A procedure call is used to invoke


the procedure. After the sequence is processed,
flow of control proceeds right after the
position
where the call was made .

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 6


Procedures

With parameters and sub-procedures (procedures of


procedures) , programs can now be written more
structured and error free.

For example, if a procedure is correct, every time it is


used it produces correct results.

Consequently, in cases of errors you can narrow your


search to those places which are not proven to be correct.

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya


7
Procedure Program view

Now a program can be viewed as a sequence of procedure


calls.

The main program is responsible to pass data to the


individual calls, the data is processed by the procedures
and the resulting data is presented.

Thus, the flow of data can be illustrated as a hierarchical


graph, a tree

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 8


Procedure Program view

Main Program

Data

Procedure1 Procedure2 Procedure3

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 9


Structured Programming
A program or program module that has one beginning and one ending, and each step in the program
execution consists of one or more of the following
• Sequence ( of program statements)
• Selections or Decisions ( where one set of statements or another executes)
• Repetition or Iteration (of a set of statements)

no no

yes

stop

Sequence Selection Repetition

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 10


Modular programming

Modular Programming is subdividing a program into separate modules


( subprogram, functions/ Procedures, methods, subroutines ). Modules
make your program shorter, hence easier to read and understand as well
as simpler to figure out how the program operates.
In Modular Programming

Each module can have its own data. This allows each module to
manage an internal state which is modified by calls to procedures or
functions of this module

Each module has its own special functionalities that supports the
implementation of the whole programming

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 11


Modular Programming

Main Program (also a Module)

Data

Module 1 Module 2
Data + Data2
Data + Data1

Procedure1 Procedure2 Procedure3

The main program coordinates calls to procedures in separate modules and


hands over appropriate data as parameters.
Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya
Modular Programming
Design Methodologies
- Functional Decomposition ( Top-Down )
- Functional composition ( Bottom- Up )

Functional Decomposition (Top-Down)


In this technique, the whole system is characterized by a single function,
and then the function is decomposed into a set of functions in a process of
stepwise refinement
The System

Function1 Function2 Function3

Function12 Function13
………
Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 13
-Top-down approach start with highest level functions-top-level and break
down into lower level program modules
Module: is a collection of instructions to accomplish some logical function or
task (modular programming) e.g. procedures / functions in a programming
language
Control program
Call module 1
Call module 2
Call module 3

Module1 Module2 Module3


Start Start Start
do1 do x If x then y
do2 do y else z
Do3 do z Return to control program
Return to control program Return to control program

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 14


Functional composition (bottom-up)
If you have different components of functions such as that from a main function, you
can compose them into a module with more significant function ( main function )

The system

function1 function2 function3

function11 function12

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 15


Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 16
Object –Oriented Programming

Thinking Methodology

• Induction (Abstract)
– From specialization to generalization
• From different dogs to create the word “dog”

Dog

17
Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya
Thinking Methodology

• Induction (Abstract)
– From specialization to generalization
• From different dogs to create the word “dog”

Dog

18
Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya
Object-Orientation

• It is a kind of thinking methodology


– Everything in the world is an object;
– Any system is composed of objects (certainly a
system is also an object);
– The evolution and development of a system is
caused by the interactions among the objects
inside or outside the system

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 19


Everything in the world is an object

• A flower, a tree, an animal


• A student, a professor
• A desk, a chair, a classroom, a building
• A university, a city, a country
• The world, the universe
• A subject such as CS, English, Math, History,

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 20


Any system is composed of objects

• A law system
• A cultural system
• An educational system
• An economic system
• An Information system
• A computer system

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 21


The development of a system is caused by
the interactions

• Cihan University is developed by the interactions


among:
– students
– professors Inside
– staffs Cihan
– officers of Sulaimanya
– officers of Iraq Outside
– … ... Cihan

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 22


Design Methodologies

• Object-Orientation is one of the


computational thinking methodologies.
• Object-Orientation is a kind of design
methodology(OOA/OOD)
– Objects are the building blocks of the
program(interface object(editor, menu, file, etc),
data managing object(db), etc.).
– Objects represent real-world abstractions within
the application.

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 23


Design Methodologies

• Object-orientation supports both


– induction: objects -> a class
• This needs the tools for OOA/OOD.
– and deduction: a class ->objects
• This needs the programmers to learn about
the class library.

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 24


Design Methodologies

• Object-orientation supports both


– Top-down:
• from a superclass to subclasses;
• complex objects to simple objects.
– Bottom-up:
• from subclasses to a superclass
• Simple objects to complex objects

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 25


Object-Orientation

• “The World is Object-Oriented”


– ----Alan Kay said.
• If you know the world, you know object-orientation.
• So, object-orientation is easy.

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 26


C++ Programming Language

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 27


Structure of C++ Program
# include < C++ library or File>
Global Variables or Functions declarations
------------------ Pre-processing Directives
------------------
#int main()
{
Local variables or Functions declarations
statement1;
statement 2;
------ Program Scope
------
------
return 0;
}

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 28


Variables and Data type
Variable: A named memory location is called a variable. A memory location is a
portion of memory to store a determined value. The variable has a specific type
( such as integer or string) that determine what can be put into this memory
location.
Variable definition: is a statement that defines a variable and definition can ( and
usually should) provide initial value.
Declaration of variables: In C++, we must first declare it specifying which data type
we want it to be followed by a valid variable identifier

type identifier ;
Ex. float number;

Variable initialization:
type identifier = initial value;
Exs. int a= 40;
string name= “SAM”;
int: string:
a: 40 Name: SAM
Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 29
Identifier:
Is a sequence of one or more letters, digits or underscore characters ( _ ).
Neither spaces or symbols can be part of an identifier. Valid identifiers have to begin with a
letter. They can also begin with an underline character ( _ ), but in some cases these may be
reserved for compiler specific keywords or external identifiers.
Identifier should not match with standard reserved keywords for C++:

C++ Keywords
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete,do,
double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline,
int, long, mutable, namespace, new, operator, private, protected, public, register,
reinterpret_cast, return, short, signed, sizeof, static, static cast, struct, switch, template, this,
throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile,
wchar_t, while

Comments:
The comments is only used to allow the programmer to insert notes or descriptions embedded
within the source code. They simply do nothing because they discarded by the compiler.

// line comments
/* block comments */

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 30


Data Type
The computer has to know what kind of data we want to store in memory
locations. The memory is organized in bytes. A byte is minimum amount of a
relatively small amount of data , one single character or small integer (0 to
255). The computer can manipulate more complex data types.

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 31


Scope of Variables
A variable can be either of global or local scope. A global variable is a
variable declared in the main body of the source code, outside all
functions, while a local variable is one declared within the body of a
function or a block

Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 32


Constants:
Constants are expressions with a fixed value to which you can’t give a new
value after it has been initialized.

const type constant_name= initial _value;


Ex.
const int max = 18;
const double val = 3.145678;
Literals:
Literals are used to express particular values within the source code of a
program.
Literal constants can be divided in integer numerals, floating-point
numerals, characters , string and Boolean values.
Ex.
max+2 // a const expression( a const int plus literal)
Dr. Falah Younis Algorithm Design _ Cihan University/ Sulaimaniya 33

You might also like