10.7——单例模式案例打印机

    科技2024-04-05  101

    #define _CRT_SECURE_NO_WARNNING #include<iostream> #include<string> using namespace std; class Printer { private: Printer() { m_Count = 0; }; Printer(const Printer& p); private: static Printer* singlePrinter; int m_Count; public: static Printer* getInstance() { return singlePrinter; } void printText(string text) { cout << text << endl; m_Count++; cout << "打印机使用的次数为:" << endl; } }; Printer* Printer::singlePrinter = new Printer; void test01() { //拿到打印机 Printer* printer = Printer::getInstance(); printer->printText("离职报告"); printer->printText("升职申请"); printer->printerText("加薪申请"); printer->printerText("入职报告"); printer->printerText("退休申请"); } int main() { test01(); system("pause"); return EXIT_SUCCESS; }
    Processed: 0.016, SQL: 9