Classes and Data Abstraction

Structures

ADT -- Abstract Data Type

A data type whose properties (domain and operations) are specified independently of any particular implementation.

Class Declaration

class TimeType
{
public:				// Public member functions
    void Set( /* in */ int hours,
              /* in */ int minutes,
              /* in */ int seconds );

    void Increment();

    void Write();		
	
private:			// Private data, default 
    int hrs;
    int mins;
    int secs;
};
Class members -- data / functions

Object Declaration

	TimeType T1, T2;

class -- type
class instance -- object

Build-in operations

member selection . T1.Increment();
assignment = T2 = T1;
Example
class Date

Class scope

The name of a member is local to the class.

Information Hiding

Specification

The class declaration serves as the public interface to the user as well as the specification describing the behavior of the data type.

Specification file: timetyp1.h

Implementation

The implementation contains all the function definitions for the class member functions and creates an abstraction barrier by hiding the concrete data representations as well as the code for the operations.

Implementation file: timetyp1.cpp

Scope resolution operator ::

	TimeType::void Increment()
	{ 
		...
	};

	members of a class refer to each other without using dot notation.

TestTime program: testtim1.cpp

Software Tools

Class Object Initiation

The TimeType class depends on the client to invoke the set function before calling any other member function.

    void Increment();
        // Precondition:
        //     The Set function has been invoked at least once

Constructors

A constructor is a member function that is implicitly invoked whenever a class object is created.

Constructors declaration

class TimeType { public: ... TimeType( int, int, int ); TimeType(); private: ... };

Constructors implementation

TimeType::TimeType( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs ) { hrs = initHrs; mins = initMins; secs = initSecs; }

Default constructor : a parameterless constructor

TimeType::TimeType()
{
    hrs = 0;
    mins = 0;
    secs = 0;
}

Constructor Invoking

	TimeType lectureTime(14, 10, 0);
	TimeType startTime;

Example: Class TimeType with Constructors

  1. specification file
  2. implementation file
  3. TestTime program

Guidelines for using Class Constructors

  1. A constructor can not return a function value.
  2. A class may provide several constructors.
  3. Parameters to a constructor are passed by placing the actual parameter list immediately after the name of the class object being declared.
  4. If a class object is declared without a parameter list:
  5. If a class has at least one constructor, and an array of class objects is declared:
    	SomeClass arr[10];
    
    then one of the constructors must be the default constructor.  This constructor
    is invoked for each element of the array.
    

Class Destructor

A destructor is implicitly invoked when a class object is destroyed.

class TimeType
{
public:
    ...

    TimeType( int, int, int );
    TimeType();
    ~TimeType();
private:
    ...
};