CprS 157 Schedule
CprS 157 Class 13
CprS 157 Class 15

Structured Types, Data Abstraction, and Classes
Chapter 11 - Class 14

  • Object-oriented Languages

    1. data abstraction

      • encapsulation

        An object consists of:
        1. identity - the name of the object
        2. state - the member data of the object
        3. behavior - the member functions of the object (operations on the object)

        A class is an abstract data type which encapsulates data and operations.

        A class is a set of objects.
        An object is a single instance of a class.

        Client software uses objects by declaring an object of a particular class and then manipulates the object through its functions.

      • information hiding

        Object entities (data and functions) that are public can be referenced from outside the object. Thus they can be used by other objects.

        Those that are private can only be referenced by the object itself. Other objects cannot use them.

        This separation of private and public entities enforces information hiding that is a key feature of object-oriented design.

        Information hiding also is enforced by separating the definition of the class (its specification) from its implementation.

        Together these create an abstraction barrier, hiding implementation details and forcing any access to be through a well defined interface.

    2. inheritance

      Inheritance is a hierarchical arrangement of classes which permits some of the properties of one to be assumed (inherited).

    3. polymorphism

      A polymorphic operation is one whose meaning depends upon the type of the object to which it is bound at run time.

      Dynamic binding is the determination of which function to call at run time.

      (Inheritance and polymorphism will be discussed later in chapter 16.)

    Reading Some Sample Programs

    // SPECIFICATION FILE (timetyp1.h)
    // (This is for the first version of the TimeType class in Chap. 11)
    // This file gives the specification
    // of a TimeType abstract data type

    // IMPLEMENTATION FILE (timetyp1.cpp)
    // This file implements the TimeType member functions

    // TestTime1 program (TestTime1.cpp)
    // This is a very simple client of the revised TimeType class
    // declared in file timetyp1.h

    // SPECIFICATION FILE (timetyp2.h)
    // (This is for the 2nd version of the TimeType class in Chap. 11.
    // It includes two class constructors.)
    // This file gives the specification
    // of a TimeType abstract data type

    // IMPLEMENTATION FILE (timetyp2.cpp)
    // This file implements the TimeType member functions

    // TestTime2 program (TestTime2.cpp)
    // This is a very simple client of the original TimeType class
    // declared in file timetyp2.h

    // SPECIFICATION FILE (datetype.h)
    // This file gives the specification of a DateType abstract data
    // type and provides an enumeration type for comparing dates

    // IMPLEMENTATION FILE (datetype.cpp)
    // This file implements the DateType member functions

    // BirthdayCalls program (BirthdayCalls.cpp)
    // A data file contains people's names, phone numbers, and birth
    // dates. This program reads a date from standard input, calculates
    // a date two weeks away, and prints the names, phone numbers, and
    // birthdays of all those in the file whose birthdays come on or
    // before the date two weeks away

      The principal raison d'etre of C++ is to support object-oriented programming. Today's readings illustrate how C++ classes encapsulate the data structures and operations necessary to implement an abstract datatype. The first project constructs the TimeType class, which implements the conventional notion of clock time, eg. 9:32 AM, and some standard operations involving time. The first implementation of TimeType lacks constructors, and hence requires the user to attend to all necessary initializations. The second implementation provides constructors, and illustrates the considerable simplification achieved by their presence. The second project implements a similar abstract datatype supporting dates, using a class called DateType.

    Programming Exercises

      Solve these exercises in class, to your own satisfaction. If called for, create the appropriate data files and test your program on your data. Demonstrate your solutions to the instructor or to the class TA. There is nothing to hand in from this activity.

    Homework For Chapter 11

      In this morning's project, we will implement a class which supports rational numbers and several of their operations. In the next class we will move on to the implementation of a class which supports complex numbers. A key component behind the notion of classes is the thought that one can isolate a commonly used abstract datatype, implement it as a class, and test and debug it once and for all. Once a correct and efficient implementation is available, it can be reused over and over in different projects with little or no change. So the concept of classes helps enormously in the encapsulation of abstract datatypes and also in the goal of reusing already existing code. Enjoy!