Programming and Problem Solving with C , 2/e - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Programming and Problem Solving with C , 2/e

Description:

Title: Programming and Problem Solving with C++, 2/e Subject: C++ OOP, public inheritance, constructor initializer, composition (containment), static vs. dynamic ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 42
Provided by: Sylv101
Category:

less

Transcript and Presenter's Notes

Title: Programming and Problem Solving with C , 2/e


1
Chapter 14 Object-Oriented Software Development
Dale/Weems/Headington
2
Chapter 14 Topics
  • Structured Programming vs. Object-Oriented
    Programming
  • Using Inheritance to Create a New C class Type
  • Using Composition (Containment) to Create a New
    C class Type
  • Static vs. Dynamic Binding of Operations to
    Objects
  • Virtual Member Functions

3
OOP vs. Structured Programming
4
Two Programming Paradigms
Structural (Procedural) Object-Oriented
PROGRAM PROGRAM
5
Object-Oriented Programming Language Features
  • 1. Data abstraction
  • 2. Inheritance of properties
  • 3. Dynamic binding of operations to objects
  • Also Encapsulation, Polymorphism

6
Abstraction
  • "A view of a problem that extracts the essential
    information
  • relevant to a particular purpose and ignores the
    remainder of
  • the information."
  • -- IEEE, 1983
  • "The essence of abstraction is to extract
    essential properties
  • while omitting inessential details."
  • -- Ross et al, 1975
  • "Abstraction is a process whereby we identify
    the important
  • aspects of a phenomenon and ignore its details."
  • -- Ghezzi et al, 1991
  • "Abstraction is generally defined as 'the
    process of
  • formulating generalized concepts by extracting
    common qualities
  • from specific examples.'"

7
OOP Terms C Equivalents
  • Object Class object or class instance
  • Instance variable Private data member
  • Method Public member function
  • Message passing Function call ( to a public
    member function )

8
What is an object?
OBJECT
set of methods (public member functions) interna
l state (values of private data members)
Operations Data
9
Inheritance
10
Inheritance Hierarchy Among Vehicles
Every car is a wheeled vehicle.
11
Inheritance
  • is a mechanism by which one class acquires
    (inherits) the properties (both data and
    operations) of another class
  • the class being inherited from is the Base Class
    (Superclass)
  • the class that inherits is the Derived Class
    (Subclass)
  • the derived class is then specialized by adding
    properties specific to it

12
Time Class
13
class Time Specification
  • // SPECIFICATION FILE ( time.h )
  • class Time
  • public
  • void Set ( int hours , int minutes ,
    int seconds )
  • void Increment ( )
  • void Write ( ) const
  • Time ( int initHrs, int initMins, int
    initSecs ) // constructor
  • Time ( ) // default
    constructor
  • private
  • int hrs
  • int mins
  • int secs

14

Class Interface Diagram
Time class
Set
Private data hrs mins secs
Increment
Write
Time
Time
15
Using Inheritance to Add Features
  • // SPECIFICATION FILE ( exttime.h)
  • include time.h
  • enum ZoneType EST, CST, MST, PST, EDT, CDT,
    MDT, PDT
  • class ExtTime public Time
    // Time is the base class
  • public
  • void Set ( int hours, int minutes,
    int seconds ,

  • ZoneType timeZone )
  • void Write ( ) const
  • ExtTime ( int initHrs , int initMins ,
    int initSecs ,
  • ZoneType initZone )
    // constructor
  • ExtTime ( ) // default
    constructor
  • private
  • ZoneType zone // added data member

16
class ExtTime public Time
  • says class Time is a public base class of the
    derived class ExtTime
  • as a result, all public members of Time (except
    constructors) are also public members of ExtTime
  • in this example, new constructors are provided,
    new data member zone is added, and member
    functions Set and Write are overridden

17

Class Interface Diagram
ExtTime class
Set
Set
Private data hrs mins secs
Increment
Increment
Write
Write
Time
ExtTime
Time
ExtTime
Private data zone
18
Client Code Using ExtTime
  • include exttime.h
  • .
  • .
  • .
  • ExtTime thisTime ( 8, 35, 0, PST )
  • ExtTime thatTime // default
    constructor called
  • thatTime.Write( ) // outputs
    000000 EST
  • cout ltlt endl
  • thatTime.Set (16, 49, 23, CDT)
  • thatTime.Write( ) // outputs
    164923 CDT
  • cout ltlt endl
  • thisTime.Increment ( )
  • thisTime.Increment ( )
  • thisTime.Write ( ) // outputs
    083502 PST
  • cout ltlt endl

19
Constructor Rules for Derived Classes
  • at run time, the base class constructor is
    implicitly called first, before the body of the
    derived classs constructor executes
  • if the base class constructor requires
    parameters, they must be passed by the derived
    classs constructor

20
Implementation of ExtTime Default Constructor
  • ExtTime ExtTime ( )
  • // Default Constructor
  • // Postcondition
  • // hrs 0 mins 0 secs
    0
  • // (via an implicit call to base
    class default constructor )
  • // zone EST
  • // Time( )
  • zone EST

21
Implementation of Another ExtTime Class
Constructor
  • ExtTime ExtTime ( / in / int
    initHrs,
  • / in / int
    initMins,
  • / in / int
    initSecs,
  • / in /
    ZoneType initZone )
  • Time (initHrs, initMins, initSecs)
    // constructor initializer
  • // Precondition 0 lt initHrs lt 23 0
    lt initMins lt 59
  • // 0 lt initSecs lt 59 initZone is
    assigned
  • // Postcondition
  • // zone initZone Time set by base class
    constructor
  • zone initZone

22
Implementation of ExtTimeSet function
  • void ExtTime Set ( / in / int
    hours,
  • / in / int
    minutes,
  • / in / int
    seconds,
  • / in /
    ZoneType time Zone )
  • // Precondition 0 lt hours lt 23 0 lt
    minutes lt 59
  • // 0 lt seconds lt 59 timeZone is
    assigned
  • // Postcondition
  • // zone timeZone Time set by base class
    function
  • Time Set (hours, minutes,
    seconds)
  • zone timeZone

23
Implementation of ExtTimeWrite Function
  • void ExtTime Write ( ) const
  • // Postcondition
  • // Time has been output in form HHMMSS ZZZ
  • // where ZZZ is the time zone
    abbreviation
  • static string zoneString8
  • EST, CST, MST, PST, EDT,
    CDT, MDT, PDT
  • Time Write ( )
  • cout ltlt ltlt zoneString zone

24
Avoiding Multiple Inclusion of Header Files
  • often several program files use the same header
    file containing typedef statements, constants, or
    class type declarations--but, it is a
    compile-time error to define the same identifier
    twice
  • this preprocessor directive syntax is used to
    avoid the compilation error that would otherwise
    occur from multiple uses of include for the same
    header file
  • ifndef Preprocessor_Identifier
  • define Preprocessor_Identifier
  • .
  • .
  • .
  • endif

25
CompositionContainmentLayering
  • Has-a
  • NOT
  • Is-a

26
Composition / Containment / Layering
  • is a mechanism by which
  • the internal data of one class
  • includes an object of another class
  • The process of building one class on top of
    another class
  • having the layering class
  • contain an object of the layered class
  • as a data member

27
A TimeCard object has a Time object
  • include time.h
  • class TimeCard
  • public
  • void Punch ( / in / int hours,
  • / in / int
    minutes,
  • / in /
    int seconds )
  • void Print ( ) const
  • TimeCard ( / in / long idNum,
  • / in / int initHrs,
  • / in / int initMins,
  • / in / int
    initSecs )
  • TimeCard ( )
  • private
  • long id
  • Time timeStamp

28

TimeCard Class
TimeCard has a Time object
Private data id timeStamp
Print . . .
Private data hrs mins secs
Set
Increment
Write . . .
TimeCard
TimeCard
29
Implementation of TimeCard Class Constructor
  • TimeCard TimeCard ( / in / long idNum,
  • / in / int initHrs,
  • / in / int initMins,
  • / in
    / int initSecs )
  • timeStamp (initHrs, initMins, initSecs)
    // constructor initializer
  • // Precondition 0 lt initHrs lt 23 0
    lt initMins lt 59
  • // 0 lt initSecs lt 59 initNum is
    assigned
  • // Postcondition
  • // id idNum timeStamp set by its
    constructor
  • id idNum

30
Order in Which Constructors are Executed
  • Given a class X,
  • if X is a derived class its base class
    constructor is executed first
  • next, constructors for member objects (if any)
    are executed (using their own default
    constructors if none is specified)
  • finally, body of Xs constructor is executed
  • See g\cp2\cpp\timecard

31
Dynamic Binding
  • Virtual Functions

32
In C . . .
  • When the type of a formal parameter is a
  • parent class, the argument used can be
  • the same type as the formal parameter,
  • or,
  • any descendant class type.

33
Static Binding
  • is the compile-time determination of which
    function to call for a particular object based on
    the type of the formal parameter
  • when pass-by-value is used, static binding occurs

34
Static Binding Is Based on Formal Parameter Type
  • void Print ( / in / Time someTime )
  • cout ltlt Time is
  • someTime.Write ( )
  • cout ltlt endl
  • CLIENT CODE OUTPUT
  • Time startTime ( 8, 30, 0 ) Time is
    083000
  • ExtTime endTime (10, 45, 0, CST) Time is
    104500
  • Print ( startTime )
  • Print ( endTime )

35
Dynamic Binding
  • is the run-time determination of which function
    to call for a particular object of a descendant
    class based on the type of the argument
  • declaring a member function to be virtual
    instructs the compiler to generate code that
    guarantees dynamic binding

36
Virtual Member Function
  • // SPECIFICATION FILE ( time.h )
  • class TimeType
  • public
  • . . .
  • virtual void Write ( ) const
    // for dynamic binding
  • . . .
  • private
  • int hrs
  • int mins
  • int secs

37
Dynamic binding requires pass-by-reference
  • void Print ( / in / Time someTime )
  • cout ltlt Time is
  • someTime.Write ( )
  • cout ltlt endl
  • CLIENT CODE OUTPUT
  • Time startTime ( 8, 30, 0 )
    Time is 083000
  • ExtTime endTime (10, 45, 0, CST)
    Time is 104500 CST
  • Print ( startTime )
  • Print ( endTime )

38
Using virtual functions in C
  • dynamic binding requires pass-by-reference when
    passing a class object to a function
  • in the declaration for a virtual function, the
    word virtual appears only in the base class
  • if a base class declares a virtual function, it
    must implement that function, even if the body is
    empty
  • a derived class is not required to re-implement a
    virtual function. If it does not, the base class
    version is used see g\cp2\cpp\virtual

39
Virtual Functions Inheritance
Function Type Interface (prototype) inherited? Implementation Inherited?
Non-Virtual (normal) Yes Yes Base class implementation cannot be changed
Virtual Yes Yes Base class impl. is default but can be changed.
Pure Virtual virtual int f()0 Yes No. No impl. is given.
40
Polymorphism
  • In object-oriented programming, the ability of
    different objects to respond, each in its own
    way, to the same message.developer.apple.com/tech
    pubs/macosx/Cocoa/ObjectiveC/7objc_glossary/chapte
    r_9_section_1.html
  • The ability of different object to respond to the
    same message in specific ways Objects can have
    very different implementations of the same
    message. In Smalltalk polymorphic behavior in
    response to messages is independent of
    inheritance.www.object-arts.com/EducationCentre/G
    lossary.htm
  • With regards to an object-oriented programming
    language, such as C, polymorphism refers to an
    object's ability to behave differently depending
    on its type. This provides a powerful means for
    making extensions to objects.www.davidgould.com/B
    ooks/Glossary.htm

41
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com