An Inheritance Example
A Military Time Class
// military time uses a 24-hour clock
class Time {
public:
Time(int hour = 0, int minute = 0, int second = 0);
void SetTime (int hour, int minute, int second);
void Increment( );
void PrintTime ( ) const;
private:
int m_hours; // 0 - 23
int m_minutes;
int m_seconds;
};
//------------------------------
Time::Time (int initHours, int initMinutes, int initSeconds)
{
// should validate
m_hours = initHours;
m_minutes = initMinutes;
m_seconds = initSeconds;
}
//----------------------------------
void Time::SetTime (int hours, int minutes, int seconds)
{
// should validate
m_hours = hours;
m_minutes = minutes;
m_seconds = seconds;
}
//--------------------------------------
// Increment time by one second
void Time::Increment ( )
{
m_seconds++;
if (m_seconds > 59) {
m_seconds = 0;
m_minutes++;
if (m_minutes > 59) {
m_minutes = 0;
m_hours++;
if (m_hours > 23)
m_hours = 0;
}
}
}
//-------------------------------------
// print time as hh:mm:ss with leading zeroes
void Time::PrintTime ( ) const
{
if (m_hours < 10)
cout << '0';
cout << m_hours << ':';
if (m_minutes < 10)
cout << '0';
cout << m_minutes << ':';
if (m_seconds < 10)
cout << '0';
cout << m_seconds;
}
A Derived Class - A Military Time Class with Time Zones
class ZonedTime: public Time
{
public:
enum ZoneType = {EST, CST, MST, PST, EDT, CDT, MDT, PDT};
ZonedTime( int hour = 0, int minute = 0, int seconde = 0, ZoneType zone = EST);
void SetZonedTime (int hour, int minute, int second, ZoneType zone);
void PrintZonedTime( ) const;
private:
ZoneType m_zone;
};
What's inherited
ZonedTime publicly inherits from Time.
Time is the base class.
ZonedTime is the derived class.
What's included in a (publicly) derived class?
- ZonedTime objects have all data members and member
functions of Time objects.
- They also have a private Zone member named m_zone (specialization).
- All of Time's public members and public functions are available to
ZonedTime objects
- Time's private members and functions are not available
to ZonedTime objects
Inherited from Time:
- SetTime( )
- Increment( )
- PrintTime( )
Not inherited:
- Time constructor
- Time destructor
- Time copy constructor
- Time assignment operator
Added by derived class ZonedTime:
- SetZonedTime( )
- PrintZonedTime( )
- m_zone
Access Summary for Public Inheritance
A base class can give a derived class direct access to some members
by placing them in the protected section.
Only derived classes (and friends) can access protected members.
class Time {
public:
// same code as before
protected:
// potential encapsulation violation!
int m_hours, m_minutes, m_seconds;
};
Rather than providing protected data members for our derived classes,
a better approach is to keep our data private and provide protected
accessors/mutators. The approach keeps our data hidden.
class Time {
public:
// same code as before
protected:
int GetHours( ) const;
int GetMinutes( ) const;
int GetSeconds( ) const;
void SetHours( int newHours );
void SetMinutes( int newMinutes );
void SetMinutes( int newSeconds );
private:
int m_hours, m_minutes, m_seconds;
};
Public in the base class
- is public in the derived class
- is accessible by derived member functions, friends,
and non-member functions
Protected in the base class
- Is protected in the derived class
- Is accessible by derived member functions and friends
Private in the base class
- is not accessible by the derived class
- Must use base class public or protected functions to access