单例模式案例-打印机案例
作者:互联网
3.1 类似首相案例
3.2 提供打印功能
3.3 提供统计打印次数功能
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
class Printer {
public:
static Printer* getInstance() {
return printer;
}
void printTxt(string str) {
cout << str << endl;
m_cout++;
cout << "打印机使用了" << m_cout << "次" << endl;
}
private:
Printer(){
m_cout = 0;
}
Printer(const Printer&p){}
static Printer * printer;
int m_cout;
};
Printer*Printer::printer = new Printer;
void test01() {
Printer *p1 = Printer::getInstance();
p1->printTxt("渣渣猫");
p1->printTxt("土拨鼠");
p1->printTxt("锯嘴葫芦");
p1->printTxt("象山水库");
p1->printTxt("珍珠泉");
}
int main(){
test01();
system("pause");
return 0;
}
标签:打印机,Printer,cout,int,printTxt,案例,p1,单例,printer 来源: https://blog.csdn.net/Scarlett2025/article/details/120693757