Review - PowerPoint PPT Presentation

Loading...

PPT – Review PowerPoint presentation | free to download - id: 24bc1b-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Review

Description:

abstraction. barrier. 15. Time Class Instance Diagrams ... Example Using Preprocessor Directive #ifndef // Time.h FOR COMPILATION THE CLASS DECLARATION IN ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 19
Provided by: sylvi177
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Review


1
Review
  • Structured data type
  • Array
  • Struct

2
onePerson
5000

.id 2037581 .name
John Smith .dept C .hour
23.6
3
struct Record
  • struct Record // declares a struct data type
  • // does not allocate memory
  • int id
  • string name
  • char dept // struct
    members
  • float hour
  • Record onePerson // declare variables of Record
  • Record anotherPerson

3
4
  • struct DateType
  • int month // Assume 1 . . 12
  • int day // Assume 1 . . 31
  • int year // Assume 1900 . . 2050
  • struct StatisticsType
  • float failRate
  • DateType lastServiced // DateType is
    a struct type
  • int downDays
  • struct MachineRec
  • int idNumber
  • string description
  • StatisticsType history //
    StatisticsType is a struct type
  • DateType purchaseDate
  • float cost

4
5
Abstract Data Type (ADT)
  • a data type whose properties (domain and
    operations) are specified (what) independently of
    any particular implementation (how)
  • FOR EXAMPLE . . .

6
ADT Specification Example
  • TYPE
  • Time
  • DOMAIN
  • Each Time value is a time in hours, minutes, and
    seconds.
  • OPERATIONS
  • Set the time
  • Print the time
  • Increment by one second
  • Compare 2 times for equality
  • Determine if one time is less than another

7
Several Possible Representations of Time
  • 3 int variables
  • 3 strings
  • 3-element int array
  • actual choice of representation depends on time,
    space, and algorithms needed to implement
    operations

10 45 27
8
class TimeType Specification
  • // SPECIFICATION FILE ( timetype.h )
  • class TimeType // declares a class data type
  • // does not allocate memory
  • public // 5 public function members
  • void set ( int hours , int mins , int
    secs )
  • void increment ( )
  • void write ( ) const
  • bool Equal ( Time otherTime ) const
  • bool LessThan ( Time otherTime )
    const
  • private // 3 private data members
  • int hour
  • int mins
  • int secs

8
9
Use of C data Type class
  • software that uses the class is called a client
  • variables of the class type are called class
    objects or class instances
  • client code uses public member functions to
    handle its class objects

10
Client Code Using Time
  • include Time.h // includes
    specification of the class
  • int main ( )
  • Time time1, time2 // declares 2 objects
    of TimeType
  • int h,m,s
  • coutltltEnter the hour, minute, and
    second\n
  • cingtgthgtgtmgtgts
  • time1.set (h, m, s )
  • time2 time1
  • time1.increment()
  • time1.write()
  • time2.write()
  • if( time1.Equal(time2))
  • coutltlt" times are equal\n"
  • if( time1.LessThan(time2))
  • coutltlt"times 1 is less than time2\n"
  • time2.set(23, 59,55)
  • coutltlt"Increment time from 235955\n"
  • for( int i 1 i lt10 i)

10
11
class represents an ADT
  • 2 kinds of class members data members and
    function members
  • class members are private by default
  • data members are generally private
  • function members are generally declared public
  • private class members can be accessed only by the
    class member functions (and friend functions),
    not by client code.

12
class Operations
  • built-in operations valid on class objects are
  • member selection using dot ( . ) operator ,
  • assignment to another class variable using (
    ),
  • pass to a function as argument
  • (by value or by reference),
  • return as value of a function
  • other operations can be defined as class member
    functions

13
2 files Generally Used for class Type
  • // SPECIFICATION FILE ( Time .h )
  • // Specifies the data and function members.
  • class TimeType
  • public
  • . . .
  • private
  • . . .
  • // IMPLEMENTATION FILE ( Time.cpp )
  • // Implements the Time member functions.

14
Information Hiding
  • Class implementation details are hidden from the
    clients view. This is called information
    hiding.
  • Public functions of a class provide the interface
    between the client code and the class objects.

client code
abstraction barrier
specification
implementation
15

Time Class Instance Diagrams
time1 time2
Set
Set
Private data hrs mins secs
Private data hrs mins secs
Increment
Increment
18 30 0
17 58 2
Write
Write
LessThan
LessThan
Equal
Equal
16
Separate Compilation and Linking of Files
specification file
implementation file
main program
include Time.h
Compiler
Compiler
Linker
17
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

18
Example Using Preprocessor Directive ifndef
  • // Time.h FOR COMPILATION THE CLASS
    DECLARATION IN
  • // SPECIFICATION FILE FILE timetype.h WILL BE
    INCLUDED ONLY ONCE
  • ifndef TIME_H
  • define TIME_H // Time.cpp
    // client.cpp
  • // IMPLEMENTATION FILE // Appointment
    program
  • class Time
  • include Time.h include
    Time.h
  • public
  • . . . . . . int main ( void
    )
  • private . . .
  • . . .
  • endif
About PowerShow.com