C++可扩展的编程模式_.docx

上传人:啊飒飒 文档编号:11614097 上传时间:2021-08-26 格式:DOCX 页数:5 大小:12.39KB
返回 下载 相关 举报
C++可扩展的编程模式_.docx_第1页
第1页 / 共5页
C++可扩展的编程模式_.docx_第2页
第2页 / 共5页
C++可扩展的编程模式_.docx_第3页
第3页 / 共5页
C++可扩展的编程模式_.docx_第4页
第4页 / 共5页
C++可扩展的编程模式_.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《C++可扩展的编程模式_.docx》由会员分享,可在线阅读,更多相关《C++可扩展的编程模式_.docx(5页珍藏版)》请在三一文库上搜索。

1、C+可扩展的编程模式_ 留意:设计原则-尽量不要去修改基类内容,有新的东西要从派生类中去实现。层层派生。派生模式使得可扩展、 #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; /钟表的几种颜色 enum Color red, blue, white, black ; /钟表的样子 enum Shape square, /方形 circle /圆形 ; /抽象钟表

2、基类。将钟表类通用方法,例如显示时间抽象出来,放在基类中。 class Clock public: inline string GetTime() /显示时间的方法通用,所以抽出来作为基类中方法 time(now); NT = localtime(now); int nHour = NT-tm_hour; int nMinute = NT-tm_min; int nSecond = NT-tm_sec; char *pTemp = new char24; memset(pTemp, 0, 24); itoa(nHour, pTemp, 10); string sHour = pTemp; /转

3、换Hour memset(pTemp, 0, 24); itoa(nMinute, pTemp, 10); string sMinute = pTemp; /转换分钟 memset(pTemp, 0, 24); itoa(nSecond, pTemp, 10); string sSecond = pTemp; /转换秒数 string sTime = sHour + : + sMinute + : + sSecond; if (pTemp) delete pTemp; pTemp = NULL; return sTime; /返回表的外观颜色 virtual int GetColor() =

4、0; /纯虚函数,抽象类不能实例化对象 private: time_t now; struct tm *NT; ; /派生类-挂钟 class WallClock : public Clock public: int GetColor() /重写派生类的虚方法 return red; /挂钟的样子,派生类自己的方法 int GetShape() return square; ; /派生类-闹钟 class AlarmClock : public Clock public: int GetColor() return black; /闹钟样子-闹钟类自己的方法 int GetShape() return circle; ; /后续闹钟有功能增加了,需要从AlarmClock类派生,而不是从Clock派生 class AlramClockExtend : public AlarmClock public: void Alarm() /添加了闹铃功能 coutAlarmendl; ; #endif 更多信息请查看IT技术专栏 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1