lecture03 (1).ppt

Upload: khan

Post on 02-Mar-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 Lecture03 (1).ppt

    1/25

    Object Oriented

    Programming

    Using C++ Programming

    Language

  • 7/26/2019 Lecture03 (1).ppt

    2/25

    RECAP

    Information hiding

    Encapsulation

    Implementation

    Interface

    Messages

    Abstraction

    Student

  • 7/26/2019 Lecture03 (1).ppt

    3/25

    Lecture #

    Classes! Abstract "ata $pes!

    Comparison %ith Structures

  • 7/26/2019 Lecture03 (1).ppt

    4/25

    Classes

    Objects ha&ing same data andbeha&ior belong to same class'

    Student

  • 7/26/2019 Lecture03 (1).ppt

    5/25

    E(ample ) Class

    Circle is an closed o&al shape

    S*uare is a closed rectangular shape

    Line is open shape %ith start andending point

    Each one is a shape

    +e sa$ these objects are instancesofthe shape class

  • 7/26/2019 Lecture03 (1).ppt

    6/25

    Inheritance

    Idea of classes leads to idea ofinheritance'

    Classes can be di&ided intosubclasses

    Car ruc, -usMotoc$cl

    e

    .ehicle

    -ase Class

    "eri&ed Class

  • 7/26/2019 Lecture03 (1).ppt

    7/25

    Reusabilit$

    Inheritance e(tends reusabilit$

    Subclasses can add more functionalit$to the base class %ith out modif$ing it'

    E(ample/

    A Class e(t0ileReader can be e(tendedto an 1ML0ileReader subclass'

  • 7/26/2019 Lecture03 (1).ppt

    8/25

    Pol$morphism

    23 4 5

    "2 3 "6 4 " 7"2! "6 and " are

    8

    Shape'"ra%9:

    "istance/Int 0eet0loat Inches

  • 7/26/2019 Lecture03 (1).ppt

    9/25

    O&erloading

    If %e use Operators to achie&ePol$morphism it is called operatorO&erloading

    3 ! ;! 4! suall$ used to store onl$ "ataInformation'

    Classes stores both data and functionalit$

  • 7/26/2019 Lecture03 (1).ppt

    13/25

    A Simple Structure

    struct part

    7

    int modelnumberint partnumber

    ?oat cost

    8

  • 7/26/2019 Lecture03 (1).ppt

    14/25

    "e=ning a Structure DS$nta(

    struct part

    7

    intmodelnumber

    int

    partnumber?oat cost

    8

    e$%ord ag D "atat$pe Fame

    "e=nitionboundar$

    Members

    Semicolon!ermination

    S$m

  • 7/26/2019 Lecture03 (1).ppt

    15/25

    "e=ning a structure &ariable

    Similarl$ as %e de=ne int or

    A ?oat &ariable

    $pe &arFamee'g' Part partA

    Memory

    G

    2H2H2H2H

    2H2H2H2H

    2H2H2H2H

    2H2H2H2H22H2H2H2

    22H2H2H2

    22H2H2H2

    22H2H2H2HHH22H22

    HHH22H22

    HHH22H22

    HHH22H22

    G

    struct part

    7int modelnumberint partnumber?oat cost

    8

  • 7/26/2019 Lecture03 (1).ppt

    16/25

    Comple( Structures

    Structures %ithin structure

    Accessing members &ariables Line Line1

    Cout Line1'-'(;cord

    struct point7int (;cordint $;cord8

    struct Line7Point APoint -8

    LinePoint (;cord $;cord

    Point (;cord $;cord

  • 7/26/2019 Lecture03 (1).ppt

    17/25

    InitialiJation of structure

    "e=nation onl$

    Line LineA- 7not recommended8

    Empt$ or Kero &alued

    Line LineA- 4 7H8 7recommended8

    InitialiJation %ith &alues

    Line LineA- 4 77!68!7!588

    Assigning &alue direct to member&ariable

    LineA-'PointA'$;cord 4

  • 7/26/2019 Lecture03 (1).ppt

    18/25

    Structures and Classes

    Structures are usuall$ used to hold data onl$ classes hold both data and functions

    In C33 structures can in fact hold both dataand functions

    Major diBerence bet%een class and astructure is that in a class members arepri&ate b$ default %hile in a structurethe$are public b$ default'

  • 7/26/2019 Lecture03 (1).ppt

    19/25

    Enumerations

    EnumerationdiBerent approach to de=ning $our o%ndata t$pe.

    +e can %rite a perfectl$ =ne program %ithoutenumeration but enumeration pro&ides a lot of ease in

    programming b$ simplif$ and clarif$ing programmingcode'

    Enumerated types works when you know in advance a

    fnite list o values'

  • 7/26/2019 Lecture03 (1).ppt

    20/25

  • 7/26/2019 Lecture03 (1).ppt

    21/25

    Clarit$ of code %ith enumsintda$2!

    s%itch 9da$2:

    7

    case 2/

    G'

    brea,

    case 6/

    G'

    brea,

    case /

    G'brea,

    GG

    8

    da$sNofN%ee,da$2!s%itch 9da$2:

    7

    case SA/

    G'

    brea,

    case S>F/

    G'

    brea,

    case MOF/

    G'

    brea,

    GG

    8

  • 7/26/2019 Lecture03 (1).ppt

    22/25

    S$nta( of declaring Enums

    Enumdeclaration de=nes the set of allnames that %ill be permissible &alues ofthe t$pe'

    Permissible &alues are called enumerators'

  • 7/26/2019 Lecture03 (1).ppt

    23/25

    Enumeration

    Enumerations internall$ treated as integers'

    -$ default in enum declaration! the =rstenumerators integer &alue is H! the second

    &alue 2! and so on' +e can also change the starting inde( or

    number of enums' e'g'

    enum Suit 7 clubs42! diamonds! hearts!spades 8

  • 7/26/2019 Lecture03 (1).ppt

    24/25

    Another E(ample

    enum direction 7 north! south! east!%est 8

    direction dir2 4 south

    cout dir2

    C33 IDO treats &ariables of enum t$pesas integers! so the output %ould be 2

  • 7/26/2019 Lecture03 (1).ppt

    25/25

    T U A