Programming paradigms

Download Report

Transcript Programming paradigms

Section 3.5
Part 2.


In modern programming, problems are
broken into modules that can be
programmed easily.
Each module is a solution to an individual
problem and each module has to interface
with other modules.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
2

A local variable:
◦ This is a variable declared within a procedure or a
function of a program and which cannot be
accessed by code outside that procedure or
function.
◦ A Local variable can only be accessed by code
within the procedure or the function where it is
declared.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
3
Cont~d

A global variable
◦ This is declared within the main program or within
a public module of a program.
◦ It can be accessed from anywhere within the source
code of the program.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
4
Cont~d


A function uses PARAMETERS to pass values
to the calling module.
Consider a VB6 program to calculate the
perimeter of a rectangle.
Public Function PerimeterOfRectangle (X As Integer, Y As Integer) As Integer
X=2*X
Y=2*Y
Formal
PerimeterOfRectangle = X + Y
parameters
End Function
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
5
Cont~d

When this function is called, it is given actual
parameters. Values must be stated.
A=4
B=5
Perimeter = PerimeterOfRectangle(A, B)

Formal
parameters
Alternatively you can call the function by
giving the values directly e.g.
Perimeter = PerimeterOfRectangle(4, 5)
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
6

We have two different ways of passing
parameters.
 Passing by value
 Passing by reference

When a parameter is passed by value,
 Only the value of the parameter is passed to the procedure
called.
 Value of the parameter can be manipulated by the procedure
called.
 When the procedure called is terminated the new value is
discarded and the value of the parameter in the calling
procedure returns to the original value
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
7

When parameters are passed by reference,
 The parameter is stored in the original location and only a
pointer (a reference to the parameter’s memory address) is
passed to the procedure called.
 Any changes made to the reference passed to the procedure
called, will remove the value of the parameter at the original
location.
 When the procedure called is terminated the new value is
available to the calling procedure
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
8

Passing by value
Procedure1
Declare a
a=5
Call Procedure2 (a)
Display a
EndProcedure1
Procedure2 (ByVal b)
b=b+2
EndProcedure2
The output of the parameter will be
the display of value 5

Passing by reference
Procedure1
Declare a
a=5
Call Procedure2 (a)
Display a
EndProcedure1
Procedure2 (ByRef b)
b=b+2
EndProcedure2
The output of the parameter will be
the display of value 7
Both of these examples are coded in Visual basic
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
9

Global variable

Main
Declare a
a=5
Call Procedure2
Display a
EndProcedure1
Main
Declare a
a=5
Call Procedure2
Display a
EndProcedure1
Procedure2 ( )
Procedure2 ( )
a=7
EndProcedure2

Local variable
Declare a
a=7
EndProcedure2
The output will be the
display of value 7

The output will be the
display of value 5
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
Also coded in Visual basic
10



When a procedure or function is called, the
computer needs to know where to return to when
the function or procedure is completed. i.e. , the
return address must be known.
Furthermore, functions and procedures may call
other functions and procedures which means that
several return addresses be stored and they must
be retrieved in the right order.
This can be achieved by using a stack.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
11
1.
2.
3.
The return address is placed on stack along with
the values of parameters when a procedure is
called. If that procedure in turn calls another
procedure the returning addresses are placed on
the stack in the order so that most recent return
address is placed on the top of the stack.
As the procedure(s) execute the parameters are
read off the stack
The return values will be passed to the statements
by taking the addresses out of those
statements
out of the stack.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
12

Consider the following
Main Prog
CALL MODULE 1
Module 1
100
Module 2
CALL MODULE 2
130
End
End
End
In this example, the numbers represent memory
addresses.
The addresses will be stored in the stack each time
a function is called and will be removed from the
stack each time a return instruction is executed.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
13




When module 1 is called by the
main program, the address 100 is
pushed onto the stack
STACK (Last in First Out)
Then when module 1 calls module
2, the address 130 is also pushed
When module 2 returns a value to
module1, the address is popped
off the stack. (The pointer is now
back at 100)
When the module1 returns the
value to the main program, then
the location 100 is popped and
the pointer is put at NULL
130
100
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
14

Object oriented languages (like JAVA, Eiffel,
Smalltalk, etc) have classes and derived
classes, and use concepts of encapsulation,
inheritance and polymorphism.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
15


In the real world, you'll often find many individual
objects all of the same kind. E.g. There may be
thousands of other bicycles in existence, all of the
same make and model. Each bicycle was built from
the same set of blueprints and therefore contains the
same components.
In object-oriented terms, we say that your bicycle is
an instance of the class of objects known as bicycles.
Therefore
 A class is the blueprint from which individual objects
are created.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
16

Classes can be derived from other classes.
The derived class is called a subclass. The
class from which it's derived is called the
superclass. The following figure illustrates
these two types of classes:
• A subclass inherits its state and behavior
from all of its ancestors.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
17


Encapsulation means that the internal representation of an
object is generally hidden from view outside of the object's
definition.
Only the object's own methods can directly manipulate its
fields.
Object
Object
An object cannot
manipulate the
data directly.
Object
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
18

To calculate the area, we CAN’T do it like this
area : = myRectangle.width * myRectangle.length;
where myRectangle is an object of type Rectangle

Class Rectangle must provide a suitable method in
order to calculate the area of myRectangle.

It can do this when an instance of the class is
instantiated.

The class must be provided with the methods to
calculate the length and width, which can then be
used to calculate the area.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
19

You can use the following methods to
calculate the length and width
integer getWidth( ) {
getWidth := width;
}//end of getWidth method.
integer getLength( ) {
getLength := length;
}//end of getLength method.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
20


myRectangle can now use these methods to get at
the width and length. However, it cannot change their
values.
To find the area we can write
myWidth := myRectangle.getWidth( );
myLength := myRectangle.getLength( );
area := myWidth * myLength;

To find the perimeter we can write
myWidth := myRectangle.getWidth( );
myLength := myRectangle.getLength( );
myPerimeter := 2 * (myWidth + myLength);
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
21



Object-oriented programming allows classes
to inherit commonly used state and behavior
from other classes.
It allows the re-use of code and the facility to
extend the data and methods without
affecting the original code.
Inheritance is the ability of a class to use the
variables and methods of a class from which
the new class is derived.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
22

Consider the class Person that has data about
a person's name and address and the
methods
 outputData( ) that outputs the name and address,
 getName( ) that returns the name and
 getAddress( ) that returns the address.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
23


If we now want a class called Employee that requires
the same data and methods as Person but also needs
to store and output an employee's National Insurance
Number (NIN).
We don’t need to rewrite the contents of Person class.
We simply create a subclass called Employee which
inherits the data and methods of class Person.
Person
Name
Employee
NINumber
Address
outputData()
getName()
outputData()
getNINumber()
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
24




The fact that when two or more classes that are
inherited from a parent class, can implement an
inherited method differently is called polymorphism.
From the previous example, both the Person class
and the Employee class have the method called
outputData().
If myPerson is an instance of Person class, we can use
myperson.outputData()
To use the outputData() method of Employee class,
we can say myEmp.outputData(). Where myEmp is an
instance of the employee subclass.
VCN CIE COMPUTING 9691/3 :::
Compiled by Benjamin Muganzi
25