Course code: CS213
Course title :
(Programming Languages Concepts)
PART: 3
Prof. Taymoor Mohamed Nazmy
Dept. of computer science, faculty of computer science, Ain Shams uni.
Ex-vice dean of post graduate studies and research Cairo, Egypt
1
Statements level control structure
2
Types of Statements
3
Slide 4
Statements Level Control
• Composition: Statements are placed in a textual sequence, and they
are executed in order.
• Alternation/Selection: Two or more sequence of statements form
alternatives, so that one of the sequences is executed.
• Iteration/Loop: A sequence of statements may be executed
repeatedly
• Jump/Branch: Control is transferred from one statement to another,
which need not necessarily be placed in a textual sequence.
Control Structures
Control structures are used by the programmer to
incorporate the desired sequence of execution of the
program.
while (i<100) Control Statement
{
fun(); Control Structure
k++;
}
5
Control Structures
7
Selection Statements
• Alternatives between two or more execution
paths
• 1- one way selector
2- Two-way selectors
3- Multiple-way selectors
One-Way Selection
If statement
Syntax:
if (expression)
statement
Expression referred to as decision maker.
Statement referred to as action statement.
9
Two-Way Selection
• “if-then-else” statement
• ALGOL 60 if:
if (boolean_expr) then
statement
else
statement
• The statements could be single or compound
11
SwitchStructures
13
Example
switch (grade)
{
case 'A': System.out.println("The grade is A.");
break;
case 'B': System.out.println("The grade is B.");
break;
case 'C': System.out.println("The grade is C.");
break;
case 'D': System.out.println("The grade is D.");
break;
case 'F': System.out.println("The grade is F.");
break;
default: System.out.println("The grade is
invalid.");
}
14
switch
Structures
Slide 15
Iteration Statements
• An iterative statement causes a collection of
statements to be executed zero, one, or more times.
• Pretest: if the condition for loop termination is
tested at the top of the loop (before the statements).
• Posttest loop: the condition for exiting the loop is
tested after the statements in the loop.
Iteration/Repetition – Pre and Post Test
Pre-test repetition
statements
true
false
condition
(while <test> do <stuff>)
true
false
statements
condition
Post-test repetition
(do <stuff> while <test>)
16
17
Loops
• Two (2) kinds of iterative loops
– Enumeration-Controlled Loop
• Executed once for every value in a finite set
– Logically-Controlled Loop
• Execute until some boolean condition
– Depends on value altered in the loop
For loop statement
18
19
While statement
20
21
22
23
24Slide 24
Branching (Jump) with goto
25
Subprograms and
implementing subprograms
26
Introduction
• Subprograms may be defined within programs, or
separately in libraries that can be used by many
programs.
• In different programming languages, a subroutine
may be called a procedure, a function, a routine,
a method, or a subprogram.
• The generic term callable unit is sometimes used.
27
Subroutines or subprogram
 The pieces of code used in this way are often called
subroutines and are common to all programming
languages (but with different names)
subroutines (Perl, FORTRAN)
functions (C,C++*,FORTRAN, Java*)
procedures (PASCAL)
 Essential for procedural or structured programming.
* object-oriented programming languages
28
Advantages of using subroutines
Saves typing → fewer lines of code →less
likely to make a mistake
 re-usable
if subroutine needs to be modified, can be changed
in only one place
other programs can use the same subroutine
can be tested separately
makes the overall structure of the program
clearer
29
Definitions :
Subprogram :
A program separate from the main program that
executes a series Of operations that occurs
multiple times during the machine cycle.
Subprogram : describes the interface to and
the actions of the subprogram abstraction .
Subprogram call : is the explicit request that
the called subprograms be executed .
31
Subroutines
• A subroutine is a block of code that is
called from different places from
within a main program or other
subroutines.
–Saves code space in that the
subroutine code does not have to be
repeated in the program areas that
need it; only the code for the
subroutine call is repeated.
• A subroutine can have zero or more parameters
that control its operation
• A subroutine may need to use local variables for
computation.
• A subroutine may pass a return value back to the
caller.
• Space in data memory must be reserved for
parameters, local variables, and the return value.
32
33
9.34
Subprograms
This is useful because the subprogram makes
programming more structural: a subprogram to
accomplish a specific task can be written once but
called many times, just like predefined procedures in the
programming language.
35
36
Nested subprograms
37
The need for variable scoping
sub
input
output
Apart from input/output
all vars needed by the
sub should appear and
disappear within the
sub.
Allows us also to use
the same names for vars
outside and inside the
sub without conflict.
39
Functional programming languages
40
41
Imperative
Object Oriented
Declarative
Functional
Programming Paradigms
F# Scheme
42
SQL as declarative PL
43
Declarative Programming
• Specifies WHAT is to be computed abstractly
• Expresses the logic of a computation without describing its control flow
• Declarative languages include
– logic programming, and
– functional programming.
4.45
Pure Functional Programming
Languages
Imperative Programming:
• Program = Algorithms + Data
Functional Programming:
• Program = Functions o Functions
What is a Program?
– A program (computation) is a transformation
from input data to output data.
Examples of FP languages
• Lisp (1960, the first functional language, has no type system)
• Hope (1970s an equational fp language)
• ML (1970s introduced Polymorphic typing systems)
• Scheme (1975, static scoping)
• Miranda (1980s equational definitions, polymorphic typing
• Haskell (introduced in 1990, all the benefits of above +
facilities for programming in the large.)
• Erlang (1995 - a general-purpose concurrent programming
language and runtime system, introduced by Ericsson)
46
47
Function Application
In mathematics, function application is denoted using
parentheses, and multiplication is often denoted using
juxtaposition or space.
f(a,b) + c d
Apply the function f to a and b, and add the result to the product of c and d.
48
Functional Programming
• A functional program is simply an expression,
and executing the program means evaluating
the expression.
–No state, i.e., variables
–No assignment
–No sequencing and repetition
What is a function?
• Functions are “self contained” pieces of code that
accomplish a specific task.
• It defines a relationship between a set of possible
inputs and a set of possible outputs —they usually
take in data, process it, and return a result.
• Once a function is written, it can be used over
and over and over again.
49
Functional style of programming
• A computing system is viewed as a function which
takes input and delivers output.
• The function transforms the input into output .
• Functions are the basic building blocks from which
programs are constructed.
• The definition of each function specifies what the
function does.
• It describes the relationship between the input and the
output of the function.
50
Why functional programming
• Programs are easy to write because the system relieves the
user from dealing with many tedious implementation
considerations such as memory management, variable
declaration, etc .
• Programs are concise (typically about 1/10 of the size of a
program in non-FPL)
• Programs are easy to understand because functional
programs have nice mathematical properties (unlike imperative
programs) .
51
functional program
A functional program: Collection of functions
A function just computes and returns a value
In fact: No program variables whose values change!
A function body: Mainly calls to other functions
53
Examples
Mathematics Haskell
f(x)
f(x,y)
f(g(x))
f(x,g(y))
f(x)g(y)
f x
f x y
f (g x)
f x (g y)
f x * g y
54
Example --- Factorial
55
Example --- Factorial (Cont.)
Functional Style : Illustration
• Definition: Equations
sumto(0) = 0
sumto(n) = n + sumto(n-1)
• Computation: Substitution and Replacement
sumto(2) = 2 + sumto (2-1)
= 2 + sumto(1)
= 2 + 1 + sumto(1-1) = 2 + 1 + sumto(0)
= 2 + 1 + 0 = …
= 3
9.57
The functional paradigm
In the functional paradigm a program is considered a
mathematical function. In this context, a function is a black
box that maps a list of inputs to a list of outputs.
A function in a functional language
58
9.59
For example, we can define a primitive function called first
that extracts the first element of a list.
It may also have a function called rest that extracts all the
elements except the first.
Extracting the third element of a list
60
Example from Scheme PL
. CAR takes a list parameter; returns the first
element of that list
e.g., (CAR '(A B C)) yields A
(CAR '((A B) C D)) yields (A B)
. CDR takes a list parameter; returns the list after
removing its first element
e.g., (CDR '(A B C)) yields (B C)
(CDR '((A B) C D)) yields (C D)
CONS takes two parameters, the first of which
can be either an atom or a list and the second
of which is a list; returns a new list that
includes the first parameter as its first element
and the second parameter as the remainder of
its result
e.g., (CONS 'A '(B C)) returns (A B C)
61
Example from Scheme PL
Example
Summing the integers 1 to 10 in Java:
total = 0;
for (i = 1; i  10; ++i)
total = total+i;
The computation method is
variable assignment.
62
Summing the integers 1 to 10 in Haskell:
sum [1..10]
The computation method is function application.
62
63
Lambda Calculus. History.
• A framework developed in 1930s by Alonzo
Church to study computations with functions.
Church wanted a minimal notation to expose
only what is essential
• Lamda l calculus provides a theoretical
framework for describing functions and their
evaluation, it is a mathematical abstraction
rather than a programming language
• Church introduced the notation
lx. x
to denote a function with formal argument x and with body
x
• Functions do not have names
– names are not essential for the computation
• Functions have a single argument
– once we understand how functions with one argument
work we can generalize to multiple args.
64
65
Why Study Lambda Calculus?
• l-calculus is the standard testbed for studying
programming language features
– Because of its minimality
– Despite its syntactic simplicity the l-calculus can
easily encode.
Lambda calculus
Lambda calculus (also written as λ-calculus) is a formal system
in mathematical logic for expressing computation based on
function.
The λ-calculus provides a simple semantics for computation,
enabling properties of computation to be studied formally.
The λ-calculus incorporates two simplifications that make this
semantics simple.
66
Lambda Expressions
A lambda expression specifies the parameter(s)
and the mapping of a function
Example: for the function cube(x) = x*x*x
lx. x * x * x
Lambda expressions describe nameless functions
Lambda expressions are applied to parameter(s) by placing
the parameter(s) after the expression
Example: (lx. x*x*x) (2) evaluated to 8
67
Functional Forms
Definition:
A higher-order function, or functional form,
is one that either takes functions as parameters or
returns a function as its result, or both.
We will use two functional forms:
1. functional composition
2. apply-to-all
68
Function Composition
Definition:
Function composition is a functional form that takes two
functions as parameters and yields a function whose value
is the first actual parameter function applied to the result
of the second actual parameter function
Form: h  f ° g
which means h (x)  f ( g (x) )
For f (x)  x + 2 and g (x)  3 * x,
h  f ° g yields (3 * x) + 2
69
Apply-to-all
Definition:
Apply-to-all is a functional form that takes a single
function as a parameter and yields a list of values
obtained by applying the given function to each element
of a list of parameters
Form: 
For h (x)  x * x
( h, (2, 3, 4) ) yields (4, 9, 16)
70
Object-Oriented Programming
OOP languages
71
72
73
75
76
Object-Oriented Programming
• Object-oriented programming combines data and behavior via
encapsulation.
• Data hiding is the ability of an object to hide data from other
objects in the program.
• Only an objects methods should be able to directly manipulate
its attributes.
• Other objects are allowed to manipulate an object’s attributes
via the object’s methods.
OOP Terminologies
– Class: the abstract data type that provides a way to create new
objects based on a defintion of an object ,
(Example: The automobile class)
– Object: A class instance, it is an entity that has methods, has
attributes and can react to events.
– Attribute – Things which describe an object; the “adjectives” of
objects.
– Derived class (subclass): A class that is defined through
inheritance from another class.
– Parent class (superclass): A class from which a new class is
derived.
– Methods: the subprograms that define the operations
on objects of a class, it can have other names such as,
behaviors, operations.
– Messages: the calls to methods. Messages have two
parts--a method name and the destination object.
- Constructors – Special methods used to create
new instances of a class,
- (Example: A Honda Civic is an instance of the
automobile class.)
78
OOP Terminologies
Classes can have two kinds of methods:
• Instance Methods: operate only on the objects of the class
• Class Methods: can perform operations on the class, and
possibly on the objects of the class
Classes can also have two kinds of variables:
• Instance Variables: every object of a class has its own set of
instance variables, which store the object’s state.
• Class Variables: belong to the class, so there is only one copy
for the class.
OOP Terminologies
What is an Object?
Real-world objects have attributes and behaviors.
Examples:
• Dog
– Attributes: breed, color, hungry, tired, etc.
– Behaviors: eating, sleeping, etc.
• Bank Account
– Attributes: account number, owner, balance
– Behaviors: withdraw, deposit
80
Classes
A class is the generic definition for a set of similar
objects
• A class can be thought of as a template used to create
a set of objects.
• A class is a static definition; a piece of code written in
a programming language.
• The objects are called instances of the class.
84
Classes - Cont’d
• Each instance will have its own distinct set of
attributes.
• Every instance of the same class will have the
same set of attributes;
– every object has the same attributes but, each
instance will have its own distinct values for those
attributes.
85
86
Account
+Owner: Person
+Ammount: double
+suspend()
+deposit(sum:double)
+withdraw(sum:double)
Classes – Example
Class
Attributes
Operations
13
Account
+Owner: Person
+Ammount: double
+suspend()
+deposit(sum:double)
+withdraw(sum:double)
87
Account
+Owner: Person
+Ammount: double
+suspend()
+deposit(sum:double)
+withdraw(sum:double)
ivanAccount
+Owner="Ivan Kolev"
+Ammount=5000.0
peterAccount
+Owner="Peter Kirov"
+Ammount=1825.33
kirilAccount
14
+Owner="Kiril Kirov"
+Ammount=25.0
Classes and
Example
Object
Objects –
Class
Object
+Ammount=1825.33
Object
kirilAccount
+Owner="Kiril Kirov"
+Ammount=25.0
peterAccount
+Owner="Peter Kirov"
Account
+Owner: Person
+Ammount: double
+suspend()
+deposit(sum:double)
+withdraw(sum:double)
ivanAccount
+Owner="Ivan Kolev"
+Ammount=5000.0
89
Bank Example
• The "account" class describes
the attributes and behaviors of
bank accounts.
• The “account” class defines two
state variables (account number
and balance) and two methods
(deposit and withdraw).
class: Account
deposit()
withdraw()
balance:
number:
90
Types of Classes
Types of Classes
Types of Classes
94
Inheritance
• One of the most important opportunities for increasing
software development productivity is the concept of
software reuse.
• Software reuse is greatly facilitated with using
inheritance, by which a new abstract data type can
inherit the data and functionality of some existing type,
and is also allowed to modify some of those entities and
add new entities.
• Inheritance is the capability of one class of things to
inherit capabilities or properties from other class
99
100
101
104
• Abstraction is simplifying complex reality by modeling
classes appropriate to the problem, and working at the most
appropriate level of inheritance for a given aspect of the
problem.
• For example, a class Car would be made up of an Engine,
Gearbox, Steering objects, and many more components.
• To build the Car class, one does not need to know how the
different components work internally, but only how to
interface with them, i.e., send messages to them, receive
messages from them, and perhaps make the different objects
composing the class interact with each other.
OOP concepts: abstraction
105
Encapsulation
When classes are defined, programmers can specify that
certain methods or state variables remain hidden inside the
class.
These variables and methods are accessible from
within the class, but not accessible outside it.
The combination of collecting all the attributes
of an object into a single class definition,
combined with the ability to hide some
definitions and type information within the class,
is known as encapsulation.
106
• Encapsulation refers to the bundling of data members
and member functions inside of a common “box”, thus
creating the notion that an object contains its state as
well as its functionalities
• Information hiding refers to the notion of choosing to
either expose or hide some of the members of a class.
• These two concepts are often misidentified.
Encapsulation is often understood as including the
notion of information hiding.
OOP concepts: encapsulation and
information hiding
107
108
Messages
• Messages are information/requests that objects
send to other objects (or to themselves).
• Message components include:
– The name of the object to receive the message.
– The name of the method to perform.
– Any parameters needed for the method.
Manager Employee
Message
To: Employee
Method: getHired
Parameters: salary = $45,000, start_date = 10/21/99
109
Benefits of Messages
Message passing supports all possible interactions between
two objects.
• Message passing is the mechanism that is used to invoke a
method of the object.
• Objects do not need to be part of the same process or on the
same machine to interact with one another.
• Message passing is a run-time behavior, thus it is not the
same as a procedure call in other languages (compile-time).
– The address of the method is determined dynamically at run-
time, as the true type of the object may not be known to the
compiler.
110
111
POLYMORPHISM
 Polymorphism means many forms (it derives from the Greek
words Poly, meaning many, and Morphos, meaning form).
 It is the ability for a message or data to be processed in more
than one form.
 It is a property by which the same message can be sent to
objects of several different classes, & each object can respond
in a different way depending on its class.
Languages that support classes but not polymorphism
are called OBJECT BASED languages.
112
113
114
End of Part 3
115

Plc part 3

  • 1.
    Course code: CS213 Coursetitle : (Programming Languages Concepts) PART: 3 Prof. Taymoor Mohamed Nazmy Dept. of computer science, faculty of computer science, Ain Shams uni. Ex-vice dean of post graduate studies and research Cairo, Egypt 1
  • 2.
  • 3.
  • 4.
    Slide 4 Statements LevelControl • Composition: Statements are placed in a textual sequence, and they are executed in order. • Alternation/Selection: Two or more sequence of statements form alternatives, so that one of the sequences is executed. • Iteration/Loop: A sequence of statements may be executed repeatedly • Jump/Branch: Control is transferred from one statement to another, which need not necessarily be placed in a textual sequence.
  • 5.
    Control Structures Control structuresare used by the programmer to incorporate the desired sequence of execution of the program. while (i<100) Control Statement { fun(); Control Structure k++; } 5
  • 7.
  • 8.
    Selection Statements • Alternativesbetween two or more execution paths • 1- one way selector 2- Two-way selectors 3- Multiple-way selectors
  • 9.
    One-Way Selection If statement Syntax: if(expression) statement Expression referred to as decision maker. Statement referred to as action statement. 9
  • 11.
    Two-Way Selection • “if-then-else”statement • ALGOL 60 if: if (boolean_expr) then statement else statement • The statements could be single or compound 11
  • 13.
  • 14.
    Example switch (grade) { case 'A':System.out.println("The grade is A."); break; case 'B': System.out.println("The grade is B."); break; case 'C': System.out.println("The grade is C."); break; case 'D': System.out.println("The grade is D."); break; case 'F': System.out.println("The grade is F."); break; default: System.out.println("The grade is invalid."); } 14 switch Structures
  • 15.
    Slide 15 Iteration Statements •An iterative statement causes a collection of statements to be executed zero, one, or more times. • Pretest: if the condition for loop termination is tested at the top of the loop (before the statements). • Posttest loop: the condition for exiting the loop is tested after the statements in the loop.
  • 16.
    Iteration/Repetition – Preand Post Test Pre-test repetition statements true false condition (while <test> do <stuff>) true false statements condition Post-test repetition (do <stuff> while <test>) 16
  • 17.
    17 Loops • Two (2)kinds of iterative loops – Enumeration-Controlled Loop • Executed once for every value in a finite set – Logically-Controlled Loop • Execute until some boolean condition – Depends on value altered in the loop
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
    Introduction • Subprograms maybe defined within programs, or separately in libraries that can be used by many programs. • In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. • The generic term callable unit is sometimes used. 27
  • 28.
    Subroutines or subprogram The pieces of code used in this way are often called subroutines and are common to all programming languages (but with different names) subroutines (Perl, FORTRAN) functions (C,C++*,FORTRAN, Java*) procedures (PASCAL)  Essential for procedural or structured programming. * object-oriented programming languages 28
  • 29.
    Advantages of usingsubroutines Saves typing → fewer lines of code →less likely to make a mistake  re-usable if subroutine needs to be modified, can be changed in only one place other programs can use the same subroutine can be tested separately makes the overall structure of the program clearer 29
  • 30.
    Definitions : Subprogram : Aprogram separate from the main program that executes a series Of operations that occurs multiple times during the machine cycle. Subprogram : describes the interface to and the actions of the subprogram abstraction . Subprogram call : is the explicit request that the called subprograms be executed .
  • 31.
    31 Subroutines • A subroutineis a block of code that is called from different places from within a main program or other subroutines. –Saves code space in that the subroutine code does not have to be repeated in the program areas that need it; only the code for the subroutine call is repeated.
  • 32.
    • A subroutinecan have zero or more parameters that control its operation • A subroutine may need to use local variables for computation. • A subroutine may pass a return value back to the caller. • Space in data memory must be reserved for parameters, local variables, and the return value. 32
  • 33.
  • 34.
    9.34 Subprograms This is usefulbecause the subprogram makes programming more structural: a subprogram to accomplish a specific task can be written once but called many times, just like predefined procedures in the programming language.
  • 35.
  • 36.
  • 37.
  • 38.
    The need forvariable scoping sub input output Apart from input/output all vars needed by the sub should appear and disappear within the sub. Allows us also to use the same names for vars outside and inside the sub without conflict.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
    Declarative Programming • SpecifiesWHAT is to be computed abstractly • Expresses the logic of a computation without describing its control flow • Declarative languages include – logic programming, and – functional programming.
  • 45.
    4.45 Pure Functional Programming Languages ImperativeProgramming: • Program = Algorithms + Data Functional Programming: • Program = Functions o Functions What is a Program? – A program (computation) is a transformation from input data to output data.
  • 46.
    Examples of FPlanguages • Lisp (1960, the first functional language, has no type system) • Hope (1970s an equational fp language) • ML (1970s introduced Polymorphic typing systems) • Scheme (1975, static scoping) • Miranda (1980s equational definitions, polymorphic typing • Haskell (introduced in 1990, all the benefits of above + facilities for programming in the large.) • Erlang (1995 - a general-purpose concurrent programming language and runtime system, introduced by Ericsson) 46
  • 47.
    47 Function Application In mathematics,function application is denoted using parentheses, and multiplication is often denoted using juxtaposition or space. f(a,b) + c d Apply the function f to a and b, and add the result to the product of c and d.
  • 48.
    48 Functional Programming • Afunctional program is simply an expression, and executing the program means evaluating the expression. –No state, i.e., variables –No assignment –No sequencing and repetition
  • 49.
    What is afunction? • Functions are “self contained” pieces of code that accomplish a specific task. • It defines a relationship between a set of possible inputs and a set of possible outputs —they usually take in data, process it, and return a result. • Once a function is written, it can be used over and over and over again. 49
  • 50.
    Functional style ofprogramming • A computing system is viewed as a function which takes input and delivers output. • The function transforms the input into output . • Functions are the basic building blocks from which programs are constructed. • The definition of each function specifies what the function does. • It describes the relationship between the input and the output of the function. 50
  • 51.
    Why functional programming •Programs are easy to write because the system relieves the user from dealing with many tedious implementation considerations such as memory management, variable declaration, etc . • Programs are concise (typically about 1/10 of the size of a program in non-FPL) • Programs are easy to understand because functional programs have nice mathematical properties (unlike imperative programs) . 51
  • 52.
    functional program A functionalprogram: Collection of functions A function just computes and returns a value In fact: No program variables whose values change! A function body: Mainly calls to other functions
  • 53.
  • 54.
  • 55.
  • 56.
    Functional Style :Illustration • Definition: Equations sumto(0) = 0 sumto(n) = n + sumto(n-1) • Computation: Substitution and Replacement sumto(2) = 2 + sumto (2-1) = 2 + sumto(1) = 2 + 1 + sumto(1-1) = 2 + 1 + sumto(0) = 2 + 1 + 0 = … = 3
  • 57.
    9.57 The functional paradigm Inthe functional paradigm a program is considered a mathematical function. In this context, a function is a black box that maps a list of inputs to a list of outputs. A function in a functional language
  • 58.
  • 59.
    9.59 For example, wecan define a primitive function called first that extracts the first element of a list. It may also have a function called rest that extracts all the elements except the first. Extracting the third element of a list
  • 60.
    60 Example from SchemePL . CAR takes a list parameter; returns the first element of that list e.g., (CAR '(A B C)) yields A (CAR '((A B) C D)) yields (A B) . CDR takes a list parameter; returns the list after removing its first element e.g., (CDR '(A B C)) yields (B C) (CDR '((A B) C D)) yields (C D)
  • 61.
    CONS takes twoparameters, the first of which can be either an atom or a list and the second of which is a list; returns a new list that includes the first parameter as its first element and the second parameter as the remainder of its result e.g., (CONS 'A '(B C)) returns (A B C) 61 Example from Scheme PL
  • 62.
    Example Summing the integers1 to 10 in Java: total = 0; for (i = 1; i  10; ++i) total = total+i; The computation method is variable assignment. 62 Summing the integers 1 to 10 in Haskell: sum [1..10] The computation method is function application. 62
  • 63.
    63 Lambda Calculus. History. •A framework developed in 1930s by Alonzo Church to study computations with functions. Church wanted a minimal notation to expose only what is essential • Lamda l calculus provides a theoretical framework for describing functions and their evaluation, it is a mathematical abstraction rather than a programming language
  • 64.
    • Church introducedthe notation lx. x to denote a function with formal argument x and with body x • Functions do not have names – names are not essential for the computation • Functions have a single argument – once we understand how functions with one argument work we can generalize to multiple args. 64
  • 65.
    65 Why Study LambdaCalculus? • l-calculus is the standard testbed for studying programming language features – Because of its minimality – Despite its syntactic simplicity the l-calculus can easily encode.
  • 66.
    Lambda calculus Lambda calculus(also written as λ-calculus) is a formal system in mathematical logic for expressing computation based on function. The λ-calculus provides a simple semantics for computation, enabling properties of computation to be studied formally. The λ-calculus incorporates two simplifications that make this semantics simple. 66
  • 67.
    Lambda Expressions A lambdaexpression specifies the parameter(s) and the mapping of a function Example: for the function cube(x) = x*x*x lx. x * x * x Lambda expressions describe nameless functions Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression Example: (lx. x*x*x) (2) evaluated to 8 67
  • 68.
    Functional Forms Definition: A higher-orderfunction, or functional form, is one that either takes functions as parameters or returns a function as its result, or both. We will use two functional forms: 1. functional composition 2. apply-to-all 68
  • 69.
    Function Composition Definition: Function compositionis a functional form that takes two functions as parameters and yields a function whose value is the first actual parameter function applied to the result of the second actual parameter function Form: h  f ° g which means h (x)  f ( g (x) ) For f (x)  x + 2 and g (x)  3 * x, h  f ° g yields (3 * x) + 2 69
  • 70.
    Apply-to-all Definition: Apply-to-all is afunctional form that takes a single function as a parameter and yields a list of values obtained by applying the given function to each element of a list of parameters Form:  For h (x)  x * x ( h, (2, 3, 4) ) yields (4, 9, 16) 70
  • 71.
  • 72.
  • 73.
  • 75.
  • 76.
    76 Object-Oriented Programming • Object-orientedprogramming combines data and behavior via encapsulation. • Data hiding is the ability of an object to hide data from other objects in the program. • Only an objects methods should be able to directly manipulate its attributes. • Other objects are allowed to manipulate an object’s attributes via the object’s methods.
  • 77.
    OOP Terminologies – Class:the abstract data type that provides a way to create new objects based on a defintion of an object , (Example: The automobile class) – Object: A class instance, it is an entity that has methods, has attributes and can react to events. – Attribute – Things which describe an object; the “adjectives” of objects. – Derived class (subclass): A class that is defined through inheritance from another class. – Parent class (superclass): A class from which a new class is derived.
  • 78.
    – Methods: thesubprograms that define the operations on objects of a class, it can have other names such as, behaviors, operations. – Messages: the calls to methods. Messages have two parts--a method name and the destination object. - Constructors – Special methods used to create new instances of a class, - (Example: A Honda Civic is an instance of the automobile class.) 78 OOP Terminologies
  • 79.
    Classes can havetwo kinds of methods: • Instance Methods: operate only on the objects of the class • Class Methods: can perform operations on the class, and possibly on the objects of the class Classes can also have two kinds of variables: • Instance Variables: every object of a class has its own set of instance variables, which store the object’s state. • Class Variables: belong to the class, so there is only one copy for the class. OOP Terminologies
  • 80.
    What is anObject? Real-world objects have attributes and behaviors. Examples: • Dog – Attributes: breed, color, hungry, tired, etc. – Behaviors: eating, sleeping, etc. • Bank Account – Attributes: account number, owner, balance – Behaviors: withdraw, deposit 80
  • 84.
    Classes A class isthe generic definition for a set of similar objects • A class can be thought of as a template used to create a set of objects. • A class is a static definition; a piece of code written in a programming language. • The objects are called instances of the class. 84
  • 85.
    Classes - Cont’d •Each instance will have its own distinct set of attributes. • Every instance of the same class will have the same set of attributes; – every object has the same attributes but, each instance will have its own distinct values for those attributes. 85
  • 86.
  • 87.
    Account +Owner: Person +Ammount: double +suspend() +deposit(sum:double) +withdraw(sum:double) Classes– Example Class Attributes Operations 13 Account +Owner: Person +Ammount: double +suspend() +deposit(sum:double) +withdraw(sum:double) 87
  • 89.
    Account +Owner: Person +Ammount: double +suspend() +deposit(sum:double) +withdraw(sum:double) ivanAccount +Owner="IvanKolev" +Ammount=5000.0 peterAccount +Owner="Peter Kirov" +Ammount=1825.33 kirilAccount 14 +Owner="Kiril Kirov" +Ammount=25.0 Classes and Example Object Objects – Class Object +Ammount=1825.33 Object kirilAccount +Owner="Kiril Kirov" +Ammount=25.0 peterAccount +Owner="Peter Kirov" Account +Owner: Person +Ammount: double +suspend() +deposit(sum:double) +withdraw(sum:double) ivanAccount +Owner="Ivan Kolev" +Ammount=5000.0 89
  • 90.
    Bank Example • The"account" class describes the attributes and behaviors of bank accounts. • The “account” class defines two state variables (account number and balance) and two methods (deposit and withdraw). class: Account deposit() withdraw() balance: number: 90
  • 91.
  • 92.
  • 93.
  • 94.
  • 97.
    Inheritance • One ofthe most important opportunities for increasing software development productivity is the concept of software reuse. • Software reuse is greatly facilitated with using inheritance, by which a new abstract data type can inherit the data and functionality of some existing type, and is also allowed to modify some of those entities and add new entities. • Inheritance is the capability of one class of things to inherit capabilities or properties from other class
  • 99.
  • 100.
  • 101.
  • 104.
  • 105.
    • Abstraction issimplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem. • For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. • To build the Car class, one does not need to know how the different components work internally, but only how to interface with them, i.e., send messages to them, receive messages from them, and perhaps make the different objects composing the class interact with each other. OOP concepts: abstraction 105
  • 106.
    Encapsulation When classes aredefined, programmers can specify that certain methods or state variables remain hidden inside the class. These variables and methods are accessible from within the class, but not accessible outside it. The combination of collecting all the attributes of an object into a single class definition, combined with the ability to hide some definitions and type information within the class, is known as encapsulation. 106
  • 107.
    • Encapsulation refersto the bundling of data members and member functions inside of a common “box”, thus creating the notion that an object contains its state as well as its functionalities • Information hiding refers to the notion of choosing to either expose or hide some of the members of a class. • These two concepts are often misidentified. Encapsulation is often understood as including the notion of information hiding. OOP concepts: encapsulation and information hiding 107
  • 108.
  • 109.
    Messages • Messages areinformation/requests that objects send to other objects (or to themselves). • Message components include: – The name of the object to receive the message. – The name of the method to perform. – Any parameters needed for the method. Manager Employee Message To: Employee Method: getHired Parameters: salary = $45,000, start_date = 10/21/99 109
  • 110.
    Benefits of Messages Messagepassing supports all possible interactions between two objects. • Message passing is the mechanism that is used to invoke a method of the object. • Objects do not need to be part of the same process or on the same machine to interact with one another. • Message passing is a run-time behavior, thus it is not the same as a procedure call in other languages (compile-time). – The address of the method is determined dynamically at run- time, as the true type of the object may not be known to the compiler. 110
  • 111.
  • 112.
    POLYMORPHISM  Polymorphism meansmany forms (it derives from the Greek words Poly, meaning many, and Morphos, meaning form).  It is the ability for a message or data to be processed in more than one form.  It is a property by which the same message can be sent to objects of several different classes, & each object can respond in a different way depending on its class. Languages that support classes but not polymorphism are called OBJECT BASED languages. 112
  • 113.
  • 114.
  • 115.