Note: Design principles----Try not to modify the base class content, there are new things to be implemented from the derived class. The layer-by-layer derivation. The derivation pattern makes it possible to extend,
#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;
Several colours of a clock
Enum Color
{
Red
Blue
White
Black
};
The shape of a clock
Enum Shape
{
Square,//square
Circle//Round
};
Abstract clock base class. The general method of the clock class, such as the display time abstraction, is placed in the base class.
Class Clock
{
Public
Inline string GetTime ()///Display time method is universal, so the extraction is used as the 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; Convert minutes
memset (ptemp, 0, 24);
Itoa (Nsecond, ptemp, 10);
string ssecond = Ptemp; Number of seconds converted
String stime = Shour + ":" + Sminute + ":" + ssecond;
if (ptemp)
{
delete [] ptemp;
Ptemp = NULL;
}
return stime;
}
Returns the appearance color of a table
virtual int GetColor () = 0; Pure virtual function, abstract class cannot instantiate object
Private
time_t now;
struct TM *nt;
};
Derived class---wall clock
Class Wallclock:public Clock
{
Public
int GetColor ()//overriding virtual methods for derived classes
{
return red;
}
The shape of the wall clock, derived classes own method
int Getshape ()
{
return square;
}
};
Derived class---alarm clock
Class Alarmclock:public Clock
{
Public
int GetColor ()
{
return to Black;
}
Alarm clock shape----alarm Clock class own method
int Getshape ()
{
return circle;
}
};
Subsequent alarms have increased functionality and need to derive from the AlarmClock class rather than from the clock
Class Alramclockextend:public AlarmClock
{
Public
void Alarm ()//Added alarm function
{
cout<< "Alarm" <<endl;
}
};
#endif