美文网首页
模版模式

模版模式

作者: 张霸天 | 来源:发表于2017-03-07 19: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();
    }
    
    
    
    

    重要概念

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

    相关文章

      网友评论

          本文标题:模版模式

          本文链接:https://www.haomeiwen.com/subject/fjxvgttx.html