/*
模板方法模式是实现一个模板父类,父类实现所有重复的代码
让子类可以修改父类维持的元素或者函数,子类可以利用多态、根据需要重写父类
*/
#include <iostream>
#include <string>
using namespace std;
typedef string(*PFUNC) ();
class TemplateClass
{
public:
virtual string Answer1()
{
return "";
}
virtual string Answer2()
{
return "";
}
public:
void TestQuestion1()
{
cout << "题目:。。。。。。。1" << endl;
}
void TestQuestion2()
{
cout << "题目2: 打发方法啊发发福利和" << endl;
}
};
class Paper1 : public TemplateClass
{
public:
virtual string Answer1()
{
return "c";
}
virtual string Answer2()
{
return "d";
}
};
class Paper2 :public TemplateClass
{
public:
virtual string Answer1()
{
return "b";
}
virtual string Answer2()
{
return "c";
}
};
void Test()
{
Paper1 *p1 = new Paper1();
Paper2 *p2 = new Paper2();
p1->TestQuestion1();
cout << p1->Answer1() << endl;
p1->TestQuestion2();
cout << p1->Answer2() << endl;
p2->TestQuestion1();
cout << p2->Answer1() << endl;
p2->TestQuestion2();
cout << p2->Answer2() << endl;
if (p1 != NULL)
{
delete p1;
p1 = NULL;
}
if (p2 != NULL)
{
delete p2;
p2 = NULL;
}
}
int main()
{
Test();
system("pause");
return EXIT_SUCCESS;
}