C ++ scalable programming mode

Source: Internet
Author: User

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

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.