Extensible programming mode for C + +

Source: Internet
Author: User
Tags abstract getcolor

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.