Working with C++ Classes

   reading: Deitel chapter 6

Time Structure in C

typedef struct
{
    int hours;
    int minutes;
    int seconds;
} Time;

void InitTime (Time* tp, int h, int m, int s); /* Must call to set up new variables */
void PrintMilitary (const Time* tp);           /* Access all three fields */
void PrintStd (const Time* tp);
void IncrementHour (Time* tp, int h);            /* Change hours field */ 
void IncrementMinutes (Time* tp, int m);       /* Change seconds field */ 
void IncrementSeconds (Time* tp, int s);       /* Change minutes field */ 

Time Class in C++

class Time {
  public:

        Time ( );                            // constructor to initialize to default time
        Time ( int h, int m, int s );   // constructor to initialize to any time

        void PrintMilitary ( void ) const;      // no need to pass parameter;
        void PrintStd ( void ) const;               //   object will be accessed automatically
        void SetTime ( int h, int m, int s );    // change all fields
        void IncrementHour ( int h );           // change hours field       
        void IncrementMinute ( int m );        // change minutes field       
        void IncrementSecond ( int s );       // change seconds field    
        int GetHour ( void )  const;                 // access hours field   
        int GetMinute ( void )  const;             // access minutes field   
        int GetSecond ( void )  const;           // access seconds field   


  private:

        int hours;      // only member functions can see these fields
        int minutes;   // prevents erroneous access / changes

        int seconds;
}

 

Some Terminology

Member functions can access private fields:

void Time :: PrintMilitary ( void )   // scope resolution operator ::
{                                             // define member functions in class file (.cpp)
    cout << (hours < 10 ? "0" : "") << hours;
    cout << ":"
    cout << (minutes < 10 ? "0" : "") << minutes;
}

Member functions can also change private fields:

void Time :: SetTime (int h, int m, int s)
{
    hours = h;    // implicit reference to object on which function was called
    minutes = m;
    seconds = s;   // should check legality of parameters
}

Non-member functions cannot do either:

void main ( )
{
    Time t;  // no-argument constructor called
     t.minutes = 11;             // both compiler errors
     cout << "The hour is: " << t.hours;
}

Data Access and Manipulation

Accessor functions: GetHour(), GetMinute(), GetSecond(), etc.

Mutator functions: SetTime(), SetHour(), SetMinute(), SetSecond(), etc.

Preserve information hiding

Constructors

Examples:

Time :: Time ( void )
{
    hours = minutes = seconds = 0;
}

Time :: Time ( int h, int m, int s )
{
    hours = ( 0 <= h  ) ? h%24 : 0;
    minutes = ( 0 <= m  ) ? m%60 : 0;
    seconds = ( 0 <= s ) ? s%60 : 0;
}

Default Arguments

Destructors

Composition (Aggregation)

this pointer

Use of const

const objects

const functions

const function parameters

Separating Interface and Implementation

Interface: header file ( Time.h )

Implementation: class file ( Time.cpp )

Good Programming Practice