#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;
}
10.7——单例模式案例打印机
最新推荐文章于 2022-05-06 18:46:08 发布