coen 244 - 3 - introduction to classes and object

Upload: maxime-pauloin

Post on 19-Feb-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    1/13

    2008 Pearson Education, Inc. All rights reserved.

    1

    3Introduction to

    Classes andObjects

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    2/13

    2008 Pearson Education, Inc. All rights reserved.

    2

    3.1 Introduction

    Programming languages may be classified intotwo categories,

    .Procedural languages :Focus is on actions ( verbs), Ex: walking,

    running. Ex. languages: C, Pascal.

    .b!ect"#riented languages: Focus is on ob!ects (nouns), Ex: cars, $lanes,

    elevators, %ouses. Ex. languages: C&&, 'ava.

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    3/13

    2008 Pearson Education, Inc. All rights reserved.

    We live in a world of objects. Object-oriented design

    (OOD) models software similar to tose tat !eo!le

    describe te real-world objects. OOD !erformsinformation iding and software reuse.

    - Objects ave attributes ( e.g. sa!e" color" si#e)

    and beavior ( e.g. ball rolls" car runs" !lane flies).

    - OOD enca!sulates attributes and o!erations

    (beavior) into objects. - In C$$" te unit of !rogramming is class wile in C is

    a function. Classes !erform information iding.

    Classes ave two t%!es of members"

    - Data members to store attributes - &unction members to store beavior

    - Classes are used to create objects.

    3

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    4/13

    2008 Pearson Education, Inc. All rights reserved.

    '%!icall% rograms will consist of"

    - &unction main

    - One or more classes

    ac class consists of two !arts*

    1. Class definition*

    - Define data members - rotot%!es of member functions

    +. Class im!lementation

    - It gives im!lementation of

    member functions

    4

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    5/13

    2008 Pearson Education, Inc. All rights reserved.

    5

    Class ,embers

    ttributes Exist t%roug%out t%e life of t%e ob!ect

    e$resented as data members

    *ariables in a class definition

    Eac% ob!ect of class maintains its own co$y of attributes

    +ocal variables

    *ariables declared in a function definitions body

    Cannot be used outside of t%at function body -%en a function terminates

    %e values of its local variables are lost

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    6/13

    2008 Pearson Education, Inc. All rights reserved.

    Class ,ember ccess !ecifiers

    ccess"s$ecifier: private

    /akes a data member or member function accessible only

    to member functions of t%e class

    privateis t%e default access for class members

    0ata %iding

    ccess"s$ecifier: $ublic

    data member or member function will be accessible also

    to t%e clients of t%e class

    eturning a value from a function function t%at s$ecifies a return ty$e ot%er t%an void

    eturns a value to its calling function

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    7/132008 Pearson Education, Inc. All rights reserved.

    !

    3./ lacing a Class in a e!arate &ile

    for 0eusabilit%

    Source code fle: Class implementation is stored in afle known as a source code fle and fle name has anextension .cpp.

    1eader files

    Class definitions are $laced in se$arate files known as

    %eader files.

    llow com$iler to recogni2e t%e classes w%en used elsew%ere

    3enerally %ave .hfilename extensions

    0river files

    Program used to test software (suc% as classes)

    Contains a mainfunction so it can be executed

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    8/13

    2008 Pearson Education,Inc. All rights reserved.

    8

    "utline

    fg03_11.cpp

    (1 of 1)

    1 / / i g. 3. 11: !r ade"ook. h

    # / / !r ade"ook cl ass def ni t i on. $hi s f l e pr esent s !rade"ook% s pu&l i c

    3 / / i nt er' ace wi t hout r eveal i ng t he i mpl ement ati ons o' !r ade"ook% smem&er

    ( / / ' unct i ons) whi ch are def ned i n !r ade"ook. cpp.

    * +i ncl ude ,st r i ng- / / cl ass !r ade"ook uses C st andard str i ng cl ass

    usi ng std: : str i ng

    2 / / !r ade"ook cl ass def ni t i on

    cl ass !r ade"ook

    10 4

    11 pu&l i c:

    1# !r ade"ook5 st r i ng 6 / / const r uct or t hat i ni t i al i 7es cour se8ame

    13 voi d setCour se8ame5 st r i ng 6 / / ' unct i on t hat sets t he cour se name

    1( st r i ng getCour se8ame5 6 / / ' unct i on t hat gets t he cour se name1* voi d di spl a9essage5 6 / / ' unct i on t hat di spl a9s a wel come message

    1 pr i vat e:

    1 st r i ng cour se8ame / / cour se name ' or t hi s !r ade"ook

    12 ; / / end cl ass !r ade"ook

    Interface contains data members

    and member function prototypes

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    9/13

    2008 Pearson Education,Inc. All rights reserved.

    #

    "utline

    fg03_1#.cpp

    (1 of 2)

    Binary scope resolution operatorties a function to its class

    GradeBookimplementation is

    placed in a separate source-code file

    Include the header file to access

    the class name GradeBook

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    10/13

    2008 Pearson Education,Inc. All rights reserved.

    10

    "utline

    fg03_1#.cpp

    (2 of 2)

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    11/13

    2008 Pearson Education,Inc. All rights reserved.

    11

    "utline

    fg03_13.cpp

    (1 of 1)

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    12/132008 Pearson Education, Inc. All rights reserved.

    12

    3. e!arating Interface from

    Im!lementation (Cont.)

    %e Com$ilation and +inking Process 4ource"code file is com$iled to create t%e classs ob!ect code

    (source"code file must +include%eader file)

    Class im$lementation $rogrammer only needs to $rovide%eader file and ob!ect code to client

    Client must +include%eader file in t%eir own code

    4o com$iler can ensure t%at t%e mainfunction creates andmani$ulates ob!ects of t%e class correctly

    o create executable a$$lication

    b!ect code for client code must be linked wit% t%e ob!ectcode for t%e class and t%e ob!ect code for any C&& 4tandard+ibrary ob!ect code used in t%e a$$lication

  • 7/23/2019 COEN 244 - 3 - Introduction to Classes and Object

    13/13 2008 Pearson Education Inc All rights reserved

    13

    &ig.3.12 Com!ilation and lin4ing !rocess tat !roduces an e5ecutable a!!lication.