Template Method design pattern of the beginner mode

 

1  / * *********************************************** **************************
 2  *********************** ************************************************** **
 3                                  program case ***
 4  a child, because of the limited conditions, teachers often are given a question on the blackboard for students to copy the first question, and then write ***
 5  answers, but now teachers are teachers send printed papers, everyone just write the answer on the line, quite ***
 6  to alleviate the problems of the students copy duplication of work ***
 7                                  core idea ***
 8                  Ba move to change the behavior of the parent class, subclass removal repeat Code ***
 9                                                                         ***
 10                                   key points ***
 11  using the parent class virtual function in the function, the answer to extend in different subclasses, i.e. three object-oriented programming Laid ***
 12 is  polymorphism (encapsulation, inheritance of resistance and polymorphic) ***
 13  ******************************************* ********************************
 14  ***************** ************************************************** ******* * / 
15 #include <the iostream>
 16 #include < String >
 . 17  the using  namespace STD;
 18 is  
. 19  class TestPaper 
20 is  {
 21 is  public :
 22 is      void to Question1 ()
 23 is      {
 24          COUT << " first question: 2 + 3 is equal to A: B 3: C. 4:. 5 D:. 6 " << endl;
 25          COUT << " answer: " answer1 << () << endl;
 26 is      };
 27      Virtual  String answer1 ()
 28      {
 29          return  "" ;
 30      };
 31 is  };
 32  
33 is  class student1:public TestPaper
 34 {
 35  public :
 36      String answer1 ()
 37 [      {
 38 is        return  " first student select A " ;
 39      }
 40  };
 41 is  
42 is  class STUDENT2: public TestPaper
 43 is  {
 44 is  public :
 45      String answer1 ()
 46 is      {
 47          return  " of two students choose B " ;
 48      }
 49  };
 50  
51 is   int main ()
 52 is  { 
53      Student1 m_student1;
54      m_student1.Question1();
55      Student2 m_student2;
56      m_student2.Question1();
57 
58      getchar();
59      return 0;
60  }

 

Output:

Guess you like

Origin www.cnblogs.com/wuhongjian/p/11567042.html