#include #include #include // 基类 CTimer class CTimer { protected: unsigned long timerID; public: CTimer(unsigned long id) : timerID(id) {} virtual ~CTimer() {} // 纯虚函数,子类必须重载 virtual std::string GetTimeZone() const = 0; unsigned long GetTimerID() const { return timerID; } }; // 子类 CBeijingTimer class CBeijingTimer : public CTimer { public: CBeijingTimer(unsigned long id) : CTimer(id) {} virtual ~CBeijingTimer() {} // 重载基类的虚函数 virtual std::string GetTimeZone() const { return "Beijing Time"; } }; int main() { std::vector timerList; // 创建一个北京时间的定时器并添加到列表中 CTimer* beijingTimer = new CBeijingTimer(1); timerList.push_back(beijingTimer); // 查询当前所有创建的Timer并打印信息 std::cout << "Timer List:" << std::endl; for (std::vector::iterator it = timerList.begin(); it != timerList.end(); ++it) { std::cout << "Timer ID: " << (*it)->GetTimerID() << ", Time Zone: " << (*it)->GetTimeZone() << std::endl; } // 清理内存 for (std::vector::iterator it = timerList.begin(); it != timerList.end(); ++it) { delete *it; } timerList.clear(); return 0; }