ZonedTime Class Implementation
ZonedTime Constructor
The base class (Time) constructor is not inherited. That means you must
write a constructor for the ZonedTime class or rely on the compiler's
default constructor for ZonedTime. A Time constructor can't create
a ZonedTime object.
The ZonedTime constructor explicitly calls a Time (base class)
constructor using a member initialization list. Without this, Time's
default constructor would be called. Note that the Time constructor
is called first, then the ZonedTime constructor body is executed.
(Destruction would take place in reverse order.)
// ZonedTime constructor
ZonedTime::ZonedTime (int hour, int minute, int second, ZoneType zone)
: Time (hour, minute, second)
{
m_zone = zone;
}
ZonedTime member functions
SetZonedTime calls public Time::SetTime( ) to set (private)
m_hours, m_minutes, and m_seconds inherited from Time.
void ZonedTime::SetZonedTime(int hour, int minute, int second, ZoneType zone)
{
SetTime (hour, minute, second); // a Time member function
m_zone = zone; // private in ZonedTime
}
Similarly, PrintZonedTime( ) calls the public Time::PrintTime( ) method to print
the (private) m_hours, m_minutes and m_seconds inherited from Time.
void ZonedTime::PrintZonedTime ( ) const
{
static string zoneString[8] =
{
"EST", "CST", "MST", "PST", "EDT", "CDT", "MDT", "PDT"
};
PrintTime ( ); // a base class method
cout << ' ' << zoneString[m_zone];
}
How ZonedTime is used
int main ( )
{
ZonedTime zTime (12, 0, 0); // noon, EST
zTime.PrintZonedTime ( ); // ZonedTime method
zTime.Increment ( ); // an inherited method
zTime.PrintTime ( ); // just prints hh:mm:ss
zTime.SetZonedTime (13, 12, 7, ZonedTime::PST);
zTime.PrintZonedTime ( ); // ZonedTime method
return 0;
}
Time and ZonedTime together
int main( )
{
Time time1(5, 14, 10);
Time *tPtr = &time1;
ZonedTime zTime1(3, 7, 12);
ZonedTime zTime2(14, 9, 20);
ZonedTime *ztPtr = &zTime1;
time1 = zTime1; // time1 is now 03:07:12
zTime2 = time1; // syntax error!
tPtr = &zTime1; // base class pointer points to derived object
tPtr->PrintTime( ); // prints 03:07:12
ztPtr = &time1; // syntax error!
return 0;
}
- A derived class object may be assigned to a base class object
because a derived object is a base object (t = zt1;)
Member slicing will occur (extra data members are lost)
- However, a base class object may not be assigned to a derived class object
(zTime2 = time1;) (it's missing some data members).
- A base class pointer may point to a derived class object.
(tPtr = &zTime1;)
- However, a derived class pointer may not (without casting)
point to a base class object. (ztPtr = &time1;)
What about the assignment operator?
Note that an overloaded base class assignment operator (operator=) is not inherited and not called automatically from the derived class overloaded assignment operator.
Time& Time::operator= (const Time& rhs)
{
// self-assignment check
if (this != &rhs)
{
m_hours = rhs.m_hours;
m_minutes= rhs.m_minutes;
m_seconds = rhs.m_seconds;
}
// enable x = y = z
return *this;
}
Erroneous assignment operator for ZonedTime
ZonedTime& ZonedTime::operator=(const ZonedTime &rhs)
{
if (this != &rhs)
m_zone = rhs.m_zone;
return *this;
}
What happens if we use this?
ZonedTime zTime1 (12, 30, 0, ZonedTime::EST);
ZonedTime zTime2 (13, 45, 30, ZonedTime::PDT);
// assignment only changes the zone
zTime2 = zTime1;
// now zTime2 is 13:45:30 EST
Correct ZonedTime assignment operator
ZonedTime&
ZonedTime::operator=(const ZonedTime &rhs)
{
if (this != &rhs)
{
// explicitly call base class operator=
Time::operator= (rhs);
m_zone = rhs.m_zone;
}
return *this;
}
Inheritance Summary
- Base class constructors and destructors are not inherited.
- The base class constructor (destructor) will automatically be invoked
when the derived class is constructed (destroyed).
- The base class constructor will be called first.
- The derived class destructor will be called first
(objects are destroyed in the reverse order they are constructed).
- Base class operator= is not inherited and not automatically
called by derived class operator=
- Friendship is not inherited. That is, a friend of the base class is
not automatically a friend of the derived class.
(Friendship must be granted.)