单例模式案例-打印机案例

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;
}

单例模式案例-打印机案例

上一篇:友链


下一篇:一个好玩的现象:getline() cannot be used after Cin directly