Programming paradigms

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual.
1 Procedural Programming Paradigm Stacks and Procedures.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Object-oriented Programming Concepts
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
C++ fundamentals.
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Abstraction ADTs, Information Hiding and Encapsulation.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object-Oriented Programming: Classes and Objects.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Introduction to Object-oriented Programming
Object-Oriented Programming
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Orientated Programming
Visit for more Learning Resources
Object Oriented Programming
OOP: Object-oriented programming
JAVA By Waqas.
Inheritance ITI1121 Nour El Kadri.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Inheritance and Polymorphism
The Object-Oriented Thought Process Chapter 1
CS 153: Concepts of Compiler Design October 5 Class Meeting
Chapter 3: Using Methods, Classes, and Objects
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Types of Programming Languages
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
3.5 Programming paradigms
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences
Object Oriented Programming
Computer Programming.
Inheritance Basics Programming with Inheritance
IFS410: Advanced Analysis and Design
PROGRAMMING PARADIGMS
Fundamentals of Programming
Week 6 Object-Oriented Programming (2): Polymorphism
Object oriented vs procedural programming
Parameter Passing Actual vs formal parameters
Computer Programming with JAVA
Java Programming, Second Edition
Object-Oriented Programming: Classes and Objects
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Workshop for Programming And Systems Management Teachers
Defining Classes and Methods
Programming Languages and Paradigms
Defining Classes and Methods
Object-Oriented PHP (1)
C++ Object Oriented 1.
Presentation transcript:

Programming paradigms Section 3.5 Programming paradigms Part 2.

Standard Programming Techniques 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

Parameters, & local and global variables 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

Parameters, & local and global variables 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

Parameters, & local and global variables 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 PerimeterOfRectangle = X + Y End Function Formal parameters VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Parameters, & local and global variables Cont~d When this function is called, it is given actual parameters. Values must be stated. A=4 B=5 Perimeter = PerimeterOfRectangle(A, B) Alternatively you can call the function by giving the values directly e.g. Perimeter = PerimeterOfRectangle(4, 5) Formal parameters VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Passing parameters 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

Passing parameters 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

Examples: Passing by value Passing by reference 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

Examples: Global variable The output will be the display of value 7 Main Declare a a = 5 Call Procedure2 Display a EndProcedure1   Procedure2 ( ) a = 7 EndProcedure2 The output will be the display of value 7 Local variable Main Declare a a = 5 Call Procedure2 Display a EndProcedure1   Procedure2 ( ) a = 7 EndProcedure2 The output will be the display of value 5 VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Also coded in Visual basic

Stacks and Procedures 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

How a stack can be used to handle procedure calling & parameter passing 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

Example: Consider the following Module 1 Module 2 End End End Main Prog Module 1 CALL 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

Example Contd STACK (Last in First Out) 130 100 When module 1 is called by the main program, the address 100 is pushed onto the stack 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 STACK (Last in First Out) 130 100 VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Object Oriented Programming 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

Classes, sub classes and super classes 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

Classes, sub classes and super classes 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

Encapsulation (data hiding) 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 method Object DATA method method An object cannot manipulate the data directly. Object VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Take an example of a class called Rectangle 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

Example Contd. 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

Example Contd. 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 myPerimeter := 2 * (myWidth + myLength); VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Inheritance 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

Example of inheritance 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

Example of inheritance Contd. 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 Employee Name Address NINumber outputData()getName() outputData()getNINumber() VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Polymorphism 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