20120224170209MTS 3033- Bab1 Introduction to OOP

download 20120224170209MTS 3033- Bab1 Introduction to OOP

of 41

Transcript of 20120224170209MTS 3033- Bab1 Introduction to OOP

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    1/41

    Click to edit Master subtitle style

    3/5/12MTS 3033 - OBJECT

    Chapter 1

    11

    Introduction to objectoriented programming

    MTS 3033 - OBJECT

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    2/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    INTRODUCTION TO OOP

    Objectives:

    Know the concept of OOP

    Know the difference between

    functional programming and OOP Know basic terminology in OOP

    Know the importance of OOP

    Know four design principles of OOP

    Know OOP programming languages

    22MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    3/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    INTRODUCTION TO OOP

    Object oriented programming isintroduced as a new programmingconcept which should help one indeveloping high quality software.

    It attempts to solve the problem withonly one approach by dividing theproblems in sub-modules and usingdifferent objects

    Objects of the program interact bysending messages to each other.

    33MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    4/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    INTRODUCTION TO OOP

    Programming with objects is quite likeworking with real world objects.

    It groups operations and data into

    modular units called objects. These objects can be combined into

    structured networks to form a completeprogram, similar to how the pieces in a

    puzzle fit together to create a picture.

    44MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    5/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    INTRODUCTION TO OOP

    By breaking down complex softwareprojects into small, self-contained andmodular units, object orientation ensurethat changes to one part of softwareproject will not affect other portions ofthe software.

    55MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    6/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    STRUCTURED vs. OOPROGRAMMING

    STRUCTURED PROGRAMMING:

    MAIN PROGRAM

    FUNCTION 3FUNCTION 2

    GLOBAL DATA

    FUNCTION 5FUNCTION 4

    FUNCTION1

    66MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    7/413/5/12MTS 3033 - OBJECT ORIENTED

    Structured Programming

    There are features of structuredprogramming:

    Emphasis is on doing things (algorithms)

    Using function

    Function & program is divided intomodules

    Every module has its own data and

    function which can be called by othermodules.

    Most of the functions share global data

    77MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    8/413/5/12MTS 3033 - OBJECT ORIENTED

    Structured Programming( Cont) Data move openly around the system

    from function to function

    Functions transform data from one form

    to another Employs top down approach in program

    design.

    88MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    9/413/5/12MTS 3033 - OBJECT ORIENTED

    OBJECT ORIENTEDPROGRAMMING

    Object1

    Object 2

    Data

    Function

    Data

    Function

    Object 3

    Data

    Function

    99MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    10/413/5/12MTS 3033 - OBJECT ORIENTED

    OBJECT ORIENTEDPROGRAMMING

    There are features of object orientedprogramming:

    Emphasis is on data rather than

    procedure Programs are divided into what are

    known as objects

    Data structures are designed such that

    they characterize the objects. Functions that operate on the data of an

    object are tied together in the datastructure

    10MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    11/413/5/12MTS 3033 - OBJECT ORIENTED

    OBJECT ORIENTEDPROGRAMMING(2)

    Data is hidden and cannot be accessedby external functions.

    Objects may communicate with each

    other through functions. New data and functions can be easily

    added whenever necessary

    Follows bottom-up approach in program

    design.

    11MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    12/413/5/12MTS 3033 - OBJECT ORIENTED

    THE DIFFERENCES BETWEENFUNCTIONAL PROGRAMMING

    AND OOPFUNCTIONAL PROGRAMMING OOP

    Emphasis is on doingthings (algorithms)

    Emphasis is on datarather than procedure

    Data and functions are keptseparately.

    Data and functions are group inclass

    Design is not very strong, hard tounderstand and difficult to

    implement.

    Design of the whole system couldbe understand by others (even

    doesnt have background onscience computer)

    Using top-down approach. Itbreaks a program down intocomponents until they cannot becomposed anymore.

    Using bottom-upapproach in programdesign

    12MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    13/413/5/12MTS 3033 - OBJECT ORIENTED

    Why chose oop?

    Current problem of software:Software is difficult to develop, maintain andmodify.

    Most software is over budget and delivered

    late. Programmers still have to create software

    from the ground-up.

    OO introduces techniques that help:

    Developing a more cost-effective and efficientsoftware that will be delivered on time.

    Adapt quickly to new changes or clientdemand.

    13MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    14/413/5/12MTS 3033 - OBJECT ORIENTED

    Pure OO Languages

    Simula

    Simulation programming language

    Found in the late 1960sBy O.J. Dahl and Kristen Nygaard

    SmalltalkIntroduced in the early 1970sBy Alan KayFirst successful object oriented

    language.

    Object-OrientedProgramming

    Languages

    14MTS 3033 - OBJECT ORIENTED

    Obj t O i t d

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    15/413/5/12MTS 3033 - OBJECT ORIENTED

    Object-OrientedProgramming

    LanguagesHybrid OO LanguagesC++, Eiffel, etc

    Emerged in the mid 1980s

    15MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    16/413/5/12MTS 3033 - OBJECT ORIENTED

    OOP offers several advantages to both programdesigner and the user. Object orientationcontributes to the solution of many problemsassociated with the development and quality

    software and lesser maintenance cost. Theprinciples advantages are :

    i. Through inheritance, we can eliminateredundant code and extend the use ofexisting class

    ii. We can build programs from the standardworking modules that communicate widthone another, rather than having to startwriting the code from scratch. This leads

    to saving development time and higher

    Advantages using oop

    16MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    17/413/5/12MTS 3033 - OBJECT ORIENTED

    iii.The principle of data hiding helpsthe programmer to build secureprograms that cannot be invadedby code in other parts of program.

    iii. It is easy to partition the work in aproject based on objects.

    iii. Object oriented system can be

    easily upgraded from small to largesystems.

    Advantages using oop

    17MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    18/413/5/12MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OFOBJECT ORIENTED

    PROGRAMMINGi. Objectii. Class

    iii. MessageObject Communication

    i. Encapsulation

    ii. Abstraction

    iii. Inheritanceiv. Polymophism

    18MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OF

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    19/413/5/12MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OFOBJECT ORIENTED

    PROGRAMMING(Cont)The key concepts are:Object

    -Directly relate to the real world

    entities.-Can be a person, thing or concept(a noun).

    -Like a black box, therefore all theimplementation is hidden.

    19MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OF

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    20/413/5/12MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OFOBJECT ORIENTED

    PROGRAMMING(Cont)Object has aAttribute - description ofobjects in a classMethod - an action performedby an object (a verb)Identity (unique name)

    20MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OF

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    21/413/5/12MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OFOBJECT ORIENTED

    PROGRAMMING(Cont)Example for attributes and methodsAttributes:

    manufacturers name

    model nameyear madecolor

    number of doorssize of engineetc.

    21MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OF

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    22/413/5/12MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OFOBJECT ORIENTED

    PROGRAMMING(Cont)Methods:Define data items (specifymanufacturers name, model,

    year, etc.)Change a data item (color,engine, etc.)Display data itemsCalculate costetc.

    22MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OF

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    23/413/5/12MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OFOBJECT ORIENTED

    PROGRAMMING (Cont) Class A generic definition for a set of

    similar objects.

    Provides the specifications forthe objects behaviors andattributes.

    An abstraction of a real worldentity.

    23MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OF

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    24/413/5/12MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OFOBJECT ORIENTED

    PROGRAMMING(Cont)Object vs Classobject is created from a class.object is considered as an instance of a

    class.class is considered as a template from

    which objects are instantiatedcan create an object or many objects from

    a class.

    24MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OF

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    25/413/5/12MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OFOBJECT ORIENTED

    PROGRAMMING(Cont)

    Diagram 1: ClassCar Car

    DoorSeatTypeModel

    DriveStopLockUnlock

    Diagram 2: MyCaras an Object

    Object vs Class

    25MTS 3033 - OBJECT ORIENTED

    BASIC CONCEPT OF

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    26/413/5/12MTS 3033 - OBJECT ORIENTED

    BASIC CONCEPT OFOBJECT ORIENTED

    PROGRAMMING(Cont) Messages

    Requests for the receiverobjects to carry out the

    indicated method or behaviorand return the result of thataction to the sender objects

    26MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OF

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    27/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    BASIC TERMINOLOGY OFOBJECT ORIENTED

    PROGRAMMING(Cont)Object Communications

    Objects communicate by sending

    messages

    27MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    28/41

    3/5/12MTS 3033 - OBJECT ORIENTED 28MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    29/41

    3/5/12MTS 3033 - OBJECT ORIENTED 29MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    30/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Design Principles of OOP

    Four main design principles of Object-Oriented Programming(OOP):

    Abstraction

    Encapsulation

    Polymorphism Inheritance

    30MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    31/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Abstraction

    Data abstraction is a process todelete all unnecessary attributes andremain the necessary attributes todescribe an object.

    Object in a program is an abstractionfrom a real object (in real world).

    Attributes characteristics, which can

    be seen. Behaviours actions that are done

    to an object.

    Figure 1.1 shows how data

    abstraction is done for class Student31MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    32/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Abstraction

    CHARACTERISTIC/ATRRIBUTES

    BEHAVIOUR/METHOD

    ABSTRACTION

    OBJECTSTUDENT

    NAME, MATRIK

    NUMBER, ADDRESS, ICNUMBER

    CALCULATE_MARK (),DETERMINE_GRED(),PRINT_RESULT()

    FIGURE 1.1

    CLASSSTUDENT

    32MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    33/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Abstraction

    OBJECT

    BOX

    ABSTRACTION

    CHARACTERISTIC/ATRRIBUTES

    BEHAVIOUR/METHOD

    OBJECT BOX

    Length, width, depth

    Calculate_Volume()Calculate_Area()

    CLASS BOX

    FIGURE 1.2

    33MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    34/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Abstraction

    Focus only on the important factsabout the problem at hand

    To design, produce and describe sothat it can be easily used without

    knowing the details of how it works.Analogy: When you drive a car, you dont

    have to know how the gasoline and

    air are mixed and ignited. Instead you only have to know how

    to use the controls.Draw map

    34MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    35/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Encapsulation

    Encapsulation is a process of tyingtogether all data and methods thatform a class and control the access todata by hiding its information.

    It enables access to object just byusing methods of that object.

    It is one of the security features inobject-oriented programming (OOP).

    Figure 1.3 shows the concept ofencapsulation for a class Student andFigure 1.4 for class Box.

    35MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    36/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Encapsulation

    Student

    private :int matricNumint icNum

    char name[30]char address[100]

    public :doublecalculate_mark()voiddetermine_mark()void print_result()

    Box

    private :float lengthfloat width

    float depth

    public :floatcalculate_volume()float area()

    FIGURE 1.3 FIGURE 1.4

    36MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    37/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Encapsulation

    Also known as data hiding Only objects methods can modify

    information in the object.

    Process of hiding the implementationdetails of an object.

    Access to manipulate the object datais through its interface (operations/

    functions). Protects an objects internal state

    from being corrupted by otherprograms.

    37MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    38/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Encapsulation

    Program maintenance is easier andless expensive because changes inthe object data or implementation isonly modified in one place

    Allows objects to be viewed as blackboxes.

    38MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    39/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Polymorphism

    the same word or phrase can meandifferent things in different contexts

    Analogy:

    In English, bankcan mean side of a

    river or a place to put moneymove -

    39MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    40/41

    3/5/12MTS 3033 - OBJECT ORIENTED

    Inheritance

    Inheritancea way of organizingclasses

    Term comes from inheritance oftraits like eye color, hair color, and so

    on. Classes with properties in common

    can be grouped so that their commonproperties are only defined once.

    Superclass inherit its attributes &methods to the subclass(es).

    Subclass can inherit all its

    superclass attributes & methods40MTS 3033 - OBJECT ORIENTED

  • 8/2/2019 20120224170209MTS 3033- Bab1 Introduction to OOP

    41/41

    Click to edit Master subtitle style

    An Inheritance Hierarchy

    Vehicle

    Automobile Motorcycle Bus

    Sedan Sports Car School BusLuxury Bus

    What properties does each vehicleinherit from the types of vehiclesabove it in the diagram?

    Superclass

    Subclasses