Introduction to Structured Data Types and Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Structured Data Types and Classes

Description:

Introduction to Structured Data Types and Classes – PowerPoint PPT presentation

Number of Views:84
Avg rating:3.0/5.0
Slides: 63
Provided by: Prefer116
Learn more at: http://cms.dt.uh.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Structured Data Types and Classes


1
Introduction to Structured Data Types and Classes
2
Objectives
  • In this chapter you will learn
  • About structured data types and C structs
  • About object-oriented programming and classes
  • About information hiding
  • How to use access specifiers
  • About interface and implementation files
  • How to prevent multiple inclusion
  • How to work with member functions

3
C Data Types
structured
simple
array struct union class
integral enum
char short int long bool
4
Structured Data Type
  • A structured data type is a type in which each
    value is a collection of component items.
  • the entire collection has a single name
  • each component can be accessed individually

5
C Structured Type
  • often we have related information of various
    types that wed like to store together for
    convenient access under the same identifier, for
    example . . .
  • Define a data types to represent time
  • Hours, mins, seconds

6
Several Possible Representations of TimeType
  • 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
7
Some Possible Representationsof ComplexNumberType
  • struct with 2 float members
  • 2-element float array

-16.2 5.8 .real .imag
-16.2 5.8
8
struct TimeType
  • struct TimeType // declares a struct data type
    does not // allocate memory
  • int hrs // struct
    members
  • int mins
  • int secs
  • TimeType StartTime // declare variables of
    TimeType
  • TimeType EndTime 12, 0, 0 //
    initializer list

8
9
struct type Declaration
  • SYNTAX
  • struct TypeName // does not allocate
    memory
  • MemberList
  • MemberList SYNTAX
  • DataType MemberName
  • DataType MemberName
  • .
  • .
  • .

10
struct type Declaration
  • The struct declaration creates a new data type
    and names the members of the struct.
  • A struct declaration creates a new data type ? a
    user-defined data type.
  • It does not allocate memory for any variables of
    that type!
  • You still need to declare your struct variables.

11
More about struct type declarations
  • If the struct type declaration precedes all
    functions it will be visible throughout the rest
    of the file. If it is placed within a function,
    only that function can use it.
  • It is common to place struct type declarations
    with TypeNames in a (.h) header file and include
    that file.
  • It is possible for members of different struct
    types to have the same identifiers. Also a
    non-struct variable may have the same identifier
    as a structure member.

12
Accessing struct Members
  • Dot ( period ) is the member selection operator.
  • After the struct type declaration, the various
    members can be used in your program only when
    they are preceded by a struct variable name and a
    dot.
  • Note initial member values are unknown
  • EXAMPLES
  • StartTime.hrs 5
  • EndTime.mins 29
  • cin gtgt EndTime.hrs

13
Aggregate Operation
  • is an operation on a data structure as a whole,
    as opposed to an operation on an individual
    component of the data structure
  • Allow use of a struct variable as a single unit

14
Aggregate struct Operations
  • Operations valid on an entire struct type
    variable
  • Assignment to another struct variable of same
    type,
  • Pass to a function as argument (by value or by
    reference),
  • Return as value of a function
  • I/O, arithmetic, and comparisons of entire struct
    variables are NOT ALLOWED!
  • Can be defined using functions

15
Defining Aggregate struct Operations
  • void WriteOut( / in / TimeType thisTime)
  • // outputs values of all members of thisTime in
    the format hrsminssecs
  • // Precondition all members of thisTime are
    assigned
  • // Postcondition all members have been
    written out
  • cout ltltthisTime.hrs ltlt ltlt thisTime.mins
  • cout ltlt ltlt thisTime.secs ltlt endl

16
Structs good or bad?
  • Allow representation of complex data that
    consists of several related fields that are of
    different types
  • Initialization (or lack of it) can cause problems
  • Only 3 aggregate ops are predefined, any other op
    must be defined as a separate function
  • Functions that operate on struct are not
    textually related to the type definition
  • Not access protection to data members

17
Abstraction
  • is the separation of the essential qualities of
    an object from the details of how it works or is
    composed
  • focuses on what, not how
  • is necessary for managing large, complex software
    projects

18
Control Abstraction
  • separates the logical properties of an action
    from its implementation
  • .
  • .
  • .
  • Search (list, item, length, where, found)
  • .
  • .
  • .
  • the function call depends on the functions
    specification (description), not its
    implementation (algorithm)

19
Data Abstraction
  • separates the logical properties of a data type
    from its implementation

LOGICAL PROPERTIES IMPLEMENTATION
What are the possible values? How can this be
done in C? What operations will be
needed? How can data types be used?
20
Data Type
set of values (domain)
allowable operations on those values
FOR EXAMPLE, data type int has
operations , -, , /, , gtgt, ltlt
domain -32768 . . . 32767
21
Abstract Data Type (ADT)
  • a data type whose properties are specified (what)
    independently of any particular implementation
    (how)
  • Data type properties include both a domain and a
    set of allowable operations
  • FOR EXAMPLE . . .

22
ADT Specification Example
  • TYPE
  • TimeType
  • DOMAIN
  • Each TimeType 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

23
ADT Implementation means
  • choosing a specific data representation for the
    abstract data using data types that already exist
    (built-in or programmer-defined)
  • writing functions for each allowable operation

24
Object-Oriented Programming and Classes
  • C allows the programmer to create Abstract Data
    Types using classes.
  • In general, classes form the basis of
    object-oriented programming
  • Example lets create a new ADT for TimeType using
    a C class

25
class TimeType Specification
  • // SPECIFICATION FILE ( timetype.h )
  • class TimeType // declares a class data type
  • // does not allocate memory
  • public // 7 public function members
  • int GetHrs() const
  • int GetMins() const
  • int GetSecs() const
  • void Set ( int hours , int mins ,
    int secs )
  • void Increment ( )
  • void Write ( ) const
  • bool LessThan ( TimeType otherTime )
    const
  • private // 3 private data members
  • int hrs
  • int mins
  • int secs

26
Classes
  • In C programming, classes are data structures
    that contain data members along with function
    members (also called member functions or
    methods)
  • The data members define the domain i.e. set of
    values that objects of that type can take.
  • The member functions define the allowable
    operations that can be used to manipulate objects
    of that type.
  • A class encapsulates all the elements of an ADT
    in a single units.
  • Note Functions that are not part of a class are
    referred to as global functions

27
Classes
  • Class definition class specification class
    implementation
  • Class specification
  • declaration as in slide 25
  • Class implementation
  • defines each member function that is declared in
    the class specification
  • Client programs
  • Programs that declare and use objects of the
    defined class
  • Client programs must include, using an include
    directive, the specification file of the class
  • TimeType T

28
Implementation of TimeType Member Functions
  • include timetype.h // Specification file
  • include ltiostreamgt
  • . . .
  • void TimeType Set ( int H, int M, int S)
  • // Postcondition data members are assigned
    new values as follows
  • // hrs H, mins M, and secs S
  • hrs H
  • mins M
  • secs S

29
Member Selection Operator
  • Member selection operator is used to access class
    members
  • T.hrs 20
  • T.Write()
  • Classes allow the programmer to define different
    types of accessibility to the members of the
    class.

30
Information Hiding
  • The principle of information hiding states that
    any class members that other programmers,
    sometimes called clients, do not need to access
    or know about should be hidden
  • Information hiding helps minimize the amount of
    information that needs to pass in and out of an
    object, which helps increase program speed and
    efficiency
  • Information hiding reduces the complexity of the
    code that clients see, allowing them to
    concentrate on the task of integrating an object
    into their programs

31
Access Specifiers
  • The first step in hiding class information is to
    set access specifiers for class members
  • Access specifiers control a clients access to
    individual data members and member functions
  • There are four levels of access specifiers
    public, private, protected, and friend
  • The public access specifier allows anyone to call
    a classs member function or to modify a data
    member

32
Access Specifiers
  • The private access specifier prevents clients
    from calling member functions or accessing data
    members and is one of the key elements in
    information hiding
  • Example assume in a client program we have
  • TimeType T
  • T.hrs 8
  • error because hrs is declared to be private
  • T.set(8, 30, 48)
  • ? OK because member function set() is declared
    public.

33
Access Specifiers
  • An access specifier that is placed on a line by
    itself followed by a colon is called an access
    label
  • Access to classes is private by default
  • Access to structs is public by default
  • Accessor functions are public member functions
    that a client can call to retrieve or modify the
    value of a data member
  • Because accessor functions often begin with the
    words get or set, they are also referred to as
    get or set functions

34

TimeType Class Instance Diagrams
currentTime endTime
35
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
36
Interface and Implementation Files
  • Interface code refers to the data member and
    member function declarations inside a class
    definitions braces
  • Interface code does not usually contain
    definitions for member functions, nor does it
    usually assign values to the data members.
  • Interface code is generally placed in a header
    file with a .h extension
  • Implementation code refers to a classs function
    definitions and any code that assigns values to a
    classs data members
  • Implementation code is placed in .cpp files
  • Implementation code must have access to the class
    specification by using a include directive
  • C source files are distributed in compiled
    format, whereas header files are distributed as
    plain text files
  • Interface code is the only code that can be seen
    by a user of the class

37
2 Separate Files Generally Used to Define a class
  • // SPECIFICATION FILE (
    timetype .h )
  • // Specifies the data and function members.
  • class TimeType
  • public
  • . . .
  • private
  • . . .
  • // IMPLEMENTATION FILE (
    timetype.cpp )
  • // Implements the TimeType member functions.
  • . . .

38
Implementation File for TimeType
  • // IMPLEMENTATION FILE (
    timetype.cpp )
  • // Implements the TimeType member functions.
  • include timetype.h // also must appear in
    client code
  • include ltiostreamgt
  • . . .
  • bool TimeType Equal ( / in / TimeType
    otherTime ) const
  • // Postcondition
  • // Function value true, if this time
    equals otherTime
  • // false
    , otherwise
  • return ( (hrs otherTime.hrs) (mins
    otherTime.mins)
  • (secs otherTime.secs) )
  • ..............
  • .

39
Example Client Code Using TimeType
  • include timetype.h // includes
    specification of the class
  • using namespace std
  • int main ( )
  • TimeType currentTime // declares 2
    objects of TimeType
  • TimeType endTime
  • bool done false
  • currentTime.Set ( 5, 30, 0 )
  • endTime.Set ( 18, 30, 0 )
  • while ( ! done )
  • . . .
  • currentTime.Increment ( )
  • if ( currentTime.Equal ( endTime ) )
  • done true

39
40
Separate Compilation and Linking of Files
specification file
implementation file
main program
include timetype.h
Compiler
Compiler
Linker
41
Modifying a Class
  • When you modify a class, interface code, such as
    class member declarations, should change the
    least
  • The implementation code normally changes the most
    when you modify a class
  • This rule of thumb is not carved in stone because
    you may find it necessary to drastically modify
    your classs interface

42
Modifying a Class
  • But for the most part, the implementation is what
    will change
  • No matter what changes you make to your
    implementation code, the changes will be
    invisible to clients if their only entry point
    into your code is the interfaceprovided that the
    interface stays the same

43
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 declarations of the same
    class
  • ifndef Preprocessor_Identifier
  • define Preprocessor_Identifier
  • .
  • . // class declaration
  • .
  • endif

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

45
Visual C pragma once Directive
  • The pragma once directive instructs the compiler
    to include a header file only once, no matter how
    many times it encounters an include statements
    for that header in other C files in the project
  • Supported in Visual C only.

46
Visual C pragma once Directive
  • // timetype .h
  • // SPECIFICATION FILE
  • pragma once
  • // timetype .cpp //
    client.cpp
  • // IMPLEMENTATION FILE // Appointment
    program
  • class TimeType
  • include timetype.h include
    timetype.h
  • public
  • . . . . . . int main ( void
    )
  • private . . .
  • . . .

47
Scope Resolution Operator ( )
  • C programs typically use several class types
  • different classes can have member functions with
    the same name, like Write( )
  • Functions from several classes can be in the same
    file. Some functions may also be global
    functions
  • in the implementation file, the scope resolution
    operator is used in the heading before the
    function members name to specify its class
  • void TimeType Write ( ) const
  • . . .

48
Use of const with Member Functions
  • when a member function does not modify the
    private data members of the class, use const in
    both the function prototype (in specification
    file) and the heading of the function definition
    (in implementation file)
  • void TimeType Write ( ) const
  • . . .

49
Example Using const with a Member Function
  • void TimeType Write ( ) const
  • // Precondition data members have been
    assigned values
  • // Postcondition Time has been output in form
    HHMMSS
  • if ( hrs lt 10 )
  • cout ltlt 0
  • cout ltlt hrs ltlt
  • if ( mins lt 10 )
  • cout ltlt 0
  • cout ltlt mins ltlt
  • if ( secs lt 10 )
  • cout ltlt 0
  • cout ltlt secs

49
50
Inline Functions
  • Functions defined inside the class body in an
    interface file are called inline functions
  • To conform to information-hiding techniques, only
    the shortest function definitions, such as
    accessor functions, should be added to the
    interface file

51
Inline Functions
  • // SPECIFICATION FILE ( timetype.h )
  • class TimeType // declares a class data type
  • // does not allocate memory
  • public // 7 public function members
  • int GetHrs() const return hrs
  • int GetMins() const return mins
  • int GetSecs() const
  • void Set ( int h , int m , int s )
  • void Increment ( )
  • void Write ( ) const
  • cout ltlthrsltltltltminsltltltltsecslt
    ltendl
  • bool LessThan ( TimeType otherTime )
    const
  • private // 3 private data members
  • int hrs
  • int mins
  • int secs

52
Inline Functions
  • // SPECIFICATION FILE ( timetype.h )
  • class TimeType // declares a class data type
  • // does not allocate memory
  • public // 7 public function members
  • int GetHrs() const return hrs
  • int GetMins() const return mins
  • int GetSecs() const
  • void Set ( int h , int m , int s )
  • void Increment ( )
  • void Write ( ) const
  • bool LessThan ( TimeType otherTime )
    const
  • private // 3 private data members
  • int hrs
  • int mins
  • int secs
  • inline void TimeTypeWrite ( ) const
  • cout ltlthrsltltltltminsltltltltsecslt
    ltendl

53
Implementation--Increment
  • void TimeTypeIncrement ( )
  • // Precondition data members have been
    assigned values
  • // Postcondition time is advanced by 1 second
    with 235959 wrapping
  • // around to 000
  • secs (secs1)60
  • if ( secs 0)
  • mins (mins 1) 60
  • if (mins 0)
  • hrs (hrs 1) 24

// all class members are accessed without
selection operator
  • Notes
  • All class members, including private members, are
    accessible from the class implementation
  • All members are accessed without the selection
    operator

54
Implementation--LessThan
  • bool TimeTypeLessThan (TimeType
    otherTime ) cont
  • // Precondition data members have been
    assigned values for both
  • // this time and otherTime
  • // Postcondition return True if this time is
    earlier than otherTime False
  • //otherwise

55
Class Constructors
  • a class constructor is a member function whose
    purpose is to initialize the private data members
    of a class object
  • a class constructor is automatically invoked when
    a class object is instantiated (i.e. declared).
  • You define a class constructor the same way you
    define any other member function, except
  • the name of a constructor is always the name of
    its class
  • there is no return type for a constructor.
  • a class may have several constructors with
    different parameter lists. A constructor with no
    parameters is the default constructor.
  • Constructors are never called explicitly. They
    are implicitly invoked.

56
Specification of TimeType Class Constructors
  • class TimeType // timetype.h
  • public // 7 function members
  • void Set ( int hours , int minutes ,
    int seconds )
  • void Increment ( )
  • void Write ( ) const
  • bool Equal ( TimeType otherTime )
    const
  • bool LessThan ( TimeType otherTime )
    const
  • TimeType ( int initHrs , int initMins ,
    int initSecs ) // constructor
  • TimeType ( ) // default
    constructor
  • private // 3 data members
  • int hrs
  • int mins
  • int secs

57
Implementation of TimeType Default Constructor
  • TimeType TimeType ( )
  • // Default Constructor
  • // Postcondition
  • // hrs 0 mins 0 secs 0
  • hrs 0
  • mins 0
  • secs 0

58
Implementation of Another TimeType Class
Constructor
  • TimeType TimeType ( / in / int initHrs,
  • / in / int initMins,
  • / in / int initSecs )
  • // Constructor
  • // Precondition 0 lt initHrs lt 23 0
    lt initMins lt 59
  • // 0 lt initSecs lt 59
  • // Postcondition
  • // hrs initHrs mins initMins secs
    initSecs
  • hrs initHrs
  • mins initMins
  • secs initSecs

59
Automatic invocation of constructors occurs
  • TimeType departureTime //
    default constructor invoked
  • TimeType movieTime (19, 30, 0 )
    // parameterized constructor
  • departureTime movieTime

Set
Set
Private data hrs mins secs
Private data hrs mins secs
Increment
Increment
0 0 0
19 30 0
Write
Write
LessThan
LessThan
Equal
Equal
60
What if no default constructor is defined ?
  • TimeType departureTime // no
    default constructor defined
  • TimeType movieTime (19, 30, 0 ) // error
    if no parameterized constructor

departureTime
61
Class Destructor--Preview
  • Invoked automatically when a class object is
    destroyed, example
  • When control leave the block in which a local
    object is declared
  • Named the same as the its class name, except that
    it is prefixed with a tilde character
  • SomeClass()
  • Only one destructor per class
  • Generally used to de-allocate space allocated
    when class object was instantiated.

62
Specification of TimeType Class Constructors
  • class TimeType // timetype.h
  • public // 7 function members
  • void Set ( int hours , int minutes ,
    int seconds )
  • void Increment ( )
  • void Write ( ) const
  • bool Equal ( TimeType otherTime )
    const
  • bool LessThan ( TimeType otherTime )
    const
  • TimeType ( int initHrs , int initMins ,
    int initSecs ) // constructor
  • TimeType ( ) // default
    constructor
  • TimeType () // class destructor
  • private // 3 data members
  • int hrs
  • int mins
  • int secs
Write a Comment
User Comments (0)
About PowerShow.com