大话设计模式 C++实现 第十章

小菜第一版

//第十章 模板方法模式
//V1
#include<iostream>
using namespace std;
class TestPaperA {
public:
	void TestQuestion1() {
		cout << "杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是[  ] a.球磨铸铁 b.马口铁 c.高速合金纲 d.铁碳纤维" << endl;
		cout << "答案:b" << endl;
	}

	void TestQuestion2() {
		cout << "杨过、程英、陆无双铲除了情花,造成[  ] a.使这种植物不再害人 b。使  一种珍惜物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化" << endl;
		cout << "答案:a" << endl;
	}

	void TestQuestion3() {
		cout << "蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[  ] a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对" << endl;
		cout << "答案:c" << endl;
	}
};

class TestPaperB {
public:
	void TestQuestion1() {
		cout << "杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是[  ] a.球磨铸铁 b.马口铁 c.高速合金纲 d.铁碳纤维" << endl;
		cout << "答案:b" << endl;
	}

	void TestQuestion2() {
		cout << "杨过、程英、陆无双铲除了情花,造成[  ] a.使这种植物不再害人 b。使  一种珍惜物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化" << endl;
		cout << "答案:a" << endl;
	}

	void TestQuestion3() {
		cout << "蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[  ] a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对" << endl;
		cout << "答案:c" << endl;
	}
};

//客户端
int main() {
	cout << "甲同学的试卷" << endl;
	TestPaperA studentA;
	studentA.TestQuestion1();
	studentA.TestQuestion2();
	studentA.TestQuestion3();

	cout << "乙同学的试卷" << endl;
	TestPaperB studentB;
	studentB.TestQuestion1();
	studentB.TestQuestion2();
	studentB.TestQuestion3();

	return 0;
}

小菜第二版

//第十章 模板方法模式
//V2
#include<iostream>
using namespace std;
class TestPaper {	//V2增加
public:
	void TestQuestion1() {
		cout << "杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是[  ] a.球磨铸铁 b.马口铁 c.高速合金纲 d.铁碳纤维" << endl;
	}

	void TestQuestion2() {
		cout << "杨过、程英、陆无双铲除了情花,造成[  ] a.使这种植物不再害人 b。使  一种珍惜物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化" << endl;
	}

	void TestQuestion3() {
		cout << "蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[  ] a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对" << endl;
	}
};


class TestPaperA : public TestPaper {
public:
	void TestQuestion1() {
		TestPaper::TestQuestion1();	//V2
		cout << "答案:b" << endl;
	}

	void TestQuestion2() {
		TestPaper::TestQuestion2();	//V2
		cout << "答案:a" << endl;
	}

	void TestQuestion3() {
		TestPaper::TestQuestion3();	//V2
		cout << "答案:c" << endl;
	}
};

class TestPaperB : public TestPaper {
public:
	void TestQuestion1() {
		TestPaper::TestQuestion1();
		cout << "答案:b" << endl;
	}

	void TestQuestion2() {
		TestPaper::TestQuestion2();
		cout << "答案:a" << endl;
	}

	void TestQuestion3() {
		TestPaper::TestQuestion3();
		cout << "答案:c" << endl;
	}
};

//客户端
int main() {
	cout << "甲同学的试卷" << endl;
	TestPaperA studentA;
	studentA.TestQuestion1();
	studentA.TestQuestion2();
	studentA.TestQuestion3();

	cout << "乙同学的试卷" << endl;
	TestPaperB studentB;
	studentB.TestQuestion1();
	studentB.TestQuestion2();
	studentB.TestQuestion3();

	return 0;
}

第三版 大鸟写

//第十章 模板方法模式
//V3
#include<iostream>
using namespace std;
class TestPaper {	//V2增加
public:
	void TestQuestion1() {
		cout << "杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是[  ] a.球磨铸铁 b.马口铁 c.高速合金纲 d.铁碳纤维" << endl;
		cout << "答案:" << Answer1() << endl;	//V3
	}

	void TestQuestion2() {
		cout << "杨过、程英、陆无双铲除了情花,造成[  ] a.使这种植物不再害人 b。使  一种珍惜物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化" << endl;
		cout << "答案:" << Answer2() << endl;	//V3
	}

	void TestQuestion3() {
		cout << "蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[  ] a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对" << endl;
		cout << "答案:" << Answer3() << endl;	//V3
	}
protected:
	virtual string Answer1() { return ""; };	//V3
	virtual string Answer2() { return ""; };	//V3
	virtual string Answer3() { return ""; };	//V3
};


class TestPaperA : public TestPaper {
protected:
	virtual string Answer1() { return "b"; };	//V3
	virtual string Answer2() { return "c"; };	//V3
	virtual string Answer3() { return "a"; };	//V3
};

class TestPaperB : public TestPaper {
protected:
	virtual string Answer1() { return "c"; };	//V3
	virtual string Answer2() { return "a"; };	//V3
	virtual string Answer3() { return "a"; };	//V3
};

//客户端
int main() {
	cout << "甲同学的试卷" << endl;
	TestPaperA studentA;
	studentA.TestQuestion1();
	studentA.TestQuestion2();
	studentA.TestQuestion3();

	cout << "乙同学的试卷" << endl;
	TestPaperB studentB;
	studentB.TestQuestion1();
	studentB.TestQuestion2();
	studentB.TestQuestion3();

	return 0;
}

由于作者水平有限,本文错漏缺点在所难免,希望读者批评指正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值