It seems that the interface for defining a method in the parent class allows the subclass to be implemented in a specific way. The implementation methods of each subclass are different.
Since inheritance is used and it is certain that this inheritance is meaningful, it should be a subclass template, and all repeated code should be raised to the parent class, instead of making every subclass repeat.
When we want to complete a process or a series of steps at a specific level of detail, but the implementation of individual steps at a more detailed level may be different, we usually consider using the template method mode for processing.
Template Method Mode: Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
1. The template method mode implements its advantages by moving the unchanged behavior to the superclass and removing repeated code in the subclass.
2. The template method mode provides a good platform for code reuse.
3. When immutable and mutable behaviors are mixed in the implementation of sub-classes of methods, immutable behaviors will be repeated in sub-classes. We use the template method Moz to move these actions to a single place, so as to help the subclass get rid of the entanglement of repeated unchanged behavior!
Let's take a look at the code. Each exam is the same, but the answers of the students are different. All of them set a virtual Answer Method for each question, you only need to write your answers.
# Pragma once <br/> # include <iostream> <br/> using namespace STD; <br/> class testpaper <br/>{< br/> public: <br/> testpaper (void) {}< br/> Public: <br/> ~ Testpaper (void) {}< br/> Public: <br/> void testquestion1 () <br/>{< br/> cout <"Yang Guo get, later, I gave Guo Jing the Xuan iron that formed Yi tianjian and Tu longdao. It may be []. ball Milling Cast Iron B. tinplate C. high-speed alloy steel D. carbon fiber "<Endl; <br/> cout <" Answer: "<answer1 () <Endl; <br/>}< br/> void testquestion2 () <br/> {<br/> cout <"Yang Guo, Cheng Ying, and Lu wushuang have wiped out the flowers, resulting in []. make this plant no longer harm B. extinction of a rare species C. destroying the ecological balance of the biosphere D. "<Endl; <br/> cout <" Answer: "<answer2 () <Endl; <br/>}< br/> void testquestion3 () <br/>{< br/> cout <"The Blue Phoenix caused the vomiting of Huashan shitu and taogu Liuxian, what medicine will you give them if you are a doctor? []. aspirin B. niuhuang Jiedu tablets C. d. let them drink a lot of raw milk E. none of the above is true "<Endl; <br/> cout <" Answer: "<answer3 () <Endl; <br/>}< br/> protected: <br/> virtual const char * answer1 () <br/>{< br/> return ""; <br/>}< br/> virtual const char * answer2 () <br/>{< br/> return ""; <br/>}< br/> virtual const char * answer3 () <br/>{< br/> return ""; <br/>}< br/> }; <br/> // exam copied by student a <br/> class testpapera: Public testpaper <br/>{< br/> protected: <br/> const char * answer1 () <br/>{< br/> return "B"; <br/>}< br/> const char * answer2 () <br/>{< br/> return "C"; <br/>}< br/> const char * answer3 () <br/>{< br/> return "A"; <br/>}< br/> }; <br/> // test paper copied by student B <br/> class testpaperb: Public testpaper <br/>{< br/> protected: <br/> const char * answer1 () <br/>{< br/> return "C"; <br/>}< br/> const char * answer2 () <br/>{< br/> return "A"; <br/>}< br/> const char * answer3 () <br/>{< br/> return "A"; <br/>}< br/>}; <br/> client implementation: <br/> # include "stdafx. H "<br/> # include" testpaper. H "<br/> # include <iostream> <br/> using namespace STD; <br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{ <br/> cout <"Student A's exam:" <Endl; <br/> testpaper * studenta = new testpapera (); <br/> studenta-> testquestion1 (); <br/> studenta-> testquestion2 (); <br/> studenta-> testquestion3 (); <br/> cout <"Student B's exam:" <Endl; <br/> testpaper * studentb = new testpaperb (); <br/> studentb-> testquestion1 (); <br/> studentb-> testquestion2 (); <br/> studentb-> testquestion3 (); <br/> cin. get (); <br/> return 0; <br/>}< br/>
The big talk design model writes a simple story, which is really easy to understand. Actually, it must be reflected in actual projects!