lecture 02 programming... bs hons.. cs

Upload: khan

Post on 02-Mar-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    1/40

    Object Oriented

    Programming

    Using C++ Programming

    Language

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    2/40

    RECAP

    Object Orientation

    A technique for system modeling

    Model

    A model is an abstractionof something Prototype/Architecture of a building

    Purpose is to understand the product beforedeveloping it

    Object

    tate !attributes"

    #ell$de%ned behavior !operations"

    &nique identity

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    3/40

    'ecture ( )

    *nformation +iding /

    Encapsulation / Abstraction /Messages / Classes

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    4/40

    *nformation +iding

    *nformation relevant to an object is

    stored/pac,ed inside it- +idden/not accessible directly for

    outside .orld-

    Only object itself can manipulatedthe data/information under controlledenvironment-

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    5/40

    Eample 0 *nformation+iding +uman

    Ahmad1s name is stored .ithin his

    brain #e can1t access his name directly

    Rather .e can as, him to tell his

    name

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    6/40

    Eample 0 *nformation+iding

    A phone stores several phone numbers

    #e can1t read the numbers directlyfrom the *M card / phone memory-

    Rather phone$set reads thisinformation for us2 3isplay in readableformat and manipulate safely

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    7/40

    Advantages of *nformation+iding

    impli%es the model by hiding itsimplementation details2 hidescompleity of inside architecture-

    Object its self handles data changes2*t restrict the change propagation

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    8/40

    Encapsulation

    3ata and behavior related together arede%ned inside an object- 4hey aretightly coupled/dependent inside anobject

    5oth the information structure andimplementation details of its operations

    are hidden from the outer .orld

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    9/40

    Eample 0 Encapsulation

    Ahmad stores his personalinformation and ,no.s ho. totranslate it to the desired language

    #e don1t ,no.

    +o. the data is stored

    +o. Ahmad translates this information

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    10/40

    Eample 0 Encapsulation

    A Phone stores phone numbers indigital format and ,no.s ho. toconvert it into human$readable

    characters

    #e don1t ,no.

    +o. the data is stored

    +o. it is converted to human$readablecharacters

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    11/40

    Encapsulation 0 Advantages

    implicity and clarity

    'o. compleity 5etter understanding

    Avoid undesirable e6ects

    ecurity

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    12/40

    Object has an *nterface

    An object encapsulates data andbehaviour

    o ho. objects interact .ith each other7

    Each object provides an interface!operations"

    Other objects communicate through thisinterface

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    13/40

    Eample 0 *nterface of a Car

    teer #heels

    Accelerate

    Change 8ear Apply 5ra,es

    4urn 'ights On/O6

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    14/40

    Eample 0 *nterface of aPhone

    *nput 9umber

    Place Call

    3isconnect Call Add number to address boo,

    end Message

    &pdate number

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    15/40

    *mplementation

    Provides services o6ered by theobject interface

    4his includes

    3ata structures to hold object state

    :unctionality that provides requiredservices

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    16/40

    Eample 0 *mplementationof teering

    3ata tructure

    Mechanical structure of steering

    :unctionality

    Mechanism to rotate tyre

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    17/40

    Eample 0 *mplementationof Address 5oo, in a Phone

    3ata tructure

    *M card *nternal Memory

    :unctionality Read/.rite circuitry

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    18/40

    eparation of *nterface ;*mplementation

    Means change in implementationdoes not e6ect object interface

    4his is achieved via principles of

    information hiding and encapsulation

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    19/40

    Eample 0 eparation of*nterface ; *mplementation

    A driver can drive a car independentof engine type !petrol2 diesel or C98"

    5ecause interface does not change

    .ith the implementation

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    20/40

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    21/40

    Advantages of eparation

    &sers need not to .orry about achange until the interface is same

    'o. Compleity !as implementationdetails are hidden"

    3irect access to information

    structure of an object can resulterrors/unstable state-

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    22/40

    Messages

    Objects communicate throughmessages

    4hey send messages !stimuli" byinvo,ing appropriate operations on thetarget object

    4he number and ,ind of messages thatcan be sent to an object depends uponits interface

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    23/40

    Eamples 0 Messages

    A Person sends message !stimulus"

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    24/40

    Abstraction

    Abstraction is a .ay to cope .ithcompleity-

    Principle of abstraction>

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    25/40

    Eample 0 Abstraction

    Attributes$ 9ame $ Employee *3

    $ tudent Roll 9o $ 3esignation

    $ ?ear of tudy $ alary$ C8PA $ Age

    Ahmad is a Ph3 student and teaches5 students

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    26/40

    Eample 0 Abstraction

    Attributes$ 9ame $ Employee *3

    $ tudent Roll 9o $ 3esignation

    $ ?ear of tudy $ alary$ C8PA $ Age

    4eacher1s Perspective

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    27/40

    Eample 0 Abstraction

    Ahmad is a Ph3 student and teaches5 students

    behaviour$ tudy $ 3evelopEam

    $ 8iveEam $ 4a,eEam

    $ Playports $ Eat$ 3eliver'ecture $ #al,

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    28/40

    Eample 0 Abstraction

    Attributes$ 9ame $ Employee *3

    $ tudent Roll 9o $ 3esignation

    $ ?ear of tudy $ alary$ C8PA $ Age

    tudent1s Perspective

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    29/40

    Eample 0 Abstraction

    tudent1s Perspective

    behaviour$ tudy $ 3evelopEam

    $ 8iveEam $ 4a,eEam

    $ Playports $ Eat$ 3eliver'ecture $ #al,

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    30/40

    Eample 0 Abstraction

    4eacher1s Perspective

    behaviour$ tudy $ 3evelopEam

    $ 8iveEam $ 4a,eEam

    $ Playports $ Eat$ 3eliver'ecture $ #al,

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    31/40

    Eample 0 Abstraction

    Ordinary PerspectiveA pet animal .ith

    :our 'egs

    A 4ail

    4.o Ears

    harp 4eeth

    urgeon1s PerspectiveA being .ith

    A ,eleton

    +eart @idney

    tomach

    A cat can be vie.ed .ith di6erentperspectives

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    32/40

    Eample 0 Abstraction

    Owners View

    Engineers View

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    33/40

    Abstraction 0 Advantages

    impli%es the model by hidingirrelevant details

    Abstraction provides the freedom to

    defer implementation decisions byavoiding commitment to details

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    34/40

    Classes

    *n an OO model2 some of the objectsehibit identical characteristics!information structure andbehaviour"

    #e say that they belong to the sameclass

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    35/40

    Eample 0 Class

    Ahmad studies mathematics

    ara studies Computers cience

    Mohsin studies chemistry

    Each one is a tudent

    #e say these objects are instancesofthe tudent class

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    36/40

    Eample 0 Class

    Circle is an closed oval shape

    quare is a closed rectangular shape

    'ine is open shape .ith start andending point

    Each one is a shape #e say these objects are instancesof

    the shape class

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    37/40

    8raphical Representation ofClasses

    !Class 9ame"

    !attributes"

    !operations"

    !Class 9ame"

    9ormal:orm

    uppressed

    :orm

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    38/40

    Eample 0 8raphicalRepresentation of Classes

    Circle

    centerradius

    dra.computeArea

    9ormal:orm

    uppressed:orm

    Circle

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    39/40

    Eample 0 8raphicalRepresentation of Classes

    Student

    name

    agegender

    tudy

    8iveEams

    9ormal:orm

    uppressed:orm

    Student

  • 7/26/2019 Lecture 02 Programming... Bs hons.. Cs

    40/40

    ; A