模版模式

2017-03-07  本文已影响0人  张霸天

#include <stdio.h>
#include <iostream>

using namespace std;

class TestPaper {
public :
    TestPaper(){};
    ~TestPaper(){};
    void TestQuestion1(){cout<<"question1"<<endl; cout<<Answer1()<<endl;};
    void TestQuestion2(){cout<<"question2"<<endl;cout<<Answer2()<<endl;};
    void TestQuestion3(){cout<<"question3"<<endl;cout<<Answer3()<<endl;};
    
    virtual string Answer1(){return "";};
    virtual string Answer2(){return "";};
    virtual string Answer3(){return "";};
};

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

class TestPaperB:public TestPaper{
public:
    string Answer1() {return "d";};
    string Answer2() {return "e";};
    string Answer3() {return "f";};
};





void testLesson7(){
    
    TestPaper * studentA = new TestPaperA();
    studentA->TestQuestion1();
    studentA->TestQuestion2();
    studentA->TestQuestion3();
    TestPaper * studentB = new TestPaperB();
    studentB->TestQuestion1();
    studentB->TestQuestion2();
    studentB->TestQuestion3();
}



重要概念

概念: 定义一个算法的基本骨架,只是微小部分不得实现推迟到子类实现,用的是继承的特性,但思想是不一样的,继承是模版模式的工具。

上一篇下一篇

猜你喜欢

热点阅读