Note: Design Principles-do not modify the content of the base class as much as possible. New things should be implemented from the derived class. Hierarchy. The derived mode enables scalability,
# Ifndef WIN_32_TEST_H
# Define WIN_32_TEST_H
# Include <iostream>
# Include <ctime>
# Include <string>
Using std: endl;
Using std: cout;
Using std: string;
// Clock colors
Enum Color
{
Red,
Blue,
White,
Black
};
// Watch shape
Enum Shape
{
Square, // square
Circle // circle
};
// Abstract The Base category of the clock. Common Methods of the clock table class, such as abstract display time, are placed in the base class.
Class Clock
{
Public:
Inline string GetTime () // The display time method is common, so it is extracted as a method in the base class
{
Time (& now );
NT = localtime (& now );
Int nHour = NT-> tm_hour;
Int nMinute = NT-> tm_min;
Int nSecond = NT-> tm_sec;
Char * pTemp = new char [24];
Memset (pTemp, 0, 24 );
Itoa (nHour, pTemp, 10 );
String sHour = pTemp; // convert Hour
Memset (pTemp, 0, 24 );
Itoa (nMinute, pTemp, 10 );
String sMinute = pTemp; // minute of conversion
Memset (pTemp, 0, 24 );
Itoa (nSecond, pTemp, 10 );
String sSecond = pTemp; // conversion seconds
String sTime = sHour + ":" + sMinute + ":" + sSecond;
If (pTemp)
{
Delete [] pTemp;
PTemp = NULL;
}
Return sTime;
}
// Returns the appearance color of the table.
Virtual int GetColor () = 0; // a pure virtual function. An abstract class cannot instantiate an object.
Private:
Time_t now;
Struct tm * NT;
};
// Derived class --- Wall Clock
Class WallClock: public Clock
{
Public:
Int GetColor () // override the virtual method of the derived class
{
Return red;
}
// The shape of the wall clock. The derived method is used.
Int GetShape ()
{
Return square;
}
};
// Derived class --- alarm clock
Class AlarmClock: public Clock
{
Public:
Int GetColor ()
{
Return black;
}
// Alert clock shape ---- alert clock Method
Int GetShape ()
{
Return circle;
}
};
// The alarm function is added in the future. It needs to be derived from the AlarmClock class, rather than from Clock.
Class AlramClockExtend: public AlarmClock
{
Public:
Void Alarm () // added the Alarm function
{
Cout <"Alarm" <endl;
}
};
# Endif