小菜第一版
//第十章 模板方法模式
//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;
}
由于作者水平有限,本文错漏缺点在所难免,希望读者批评指正