/*Student_Template.cpp Example for Template Method Pattern (GoF) Feza BUZLUCA, Lecture Notes, Slide 10.38 - */ #include using std::cout; using std::endl; /* Abstract base class */ class Student{ public: void createReport(); // Template Method protected: virtual void calculateAverage() = 0; // Abstract primitive operation, pure virtual function virtual void printReport() = 0; // Abstract primitive operation, pure virtual function }; void Student::createReport() // Template Method: Skeleton of the algorithm { // Step 1, common for all types cout << "Read Courses from a database (common for all students)" << endl; // Step 1 calculateAverage(); // Step 2, specific to different types printReport(); // Step 3, specific to different types } //-------- Subtype: Undergraduate Student ---------- class UnderGradStudent : public Student{ private: // It can also be protected void calculateAverage(){ // Concrete primitive function, specific to Undergraduate Students cout << "Average of the Undergraduate Student" << endl; } void printReport(){ // Concrete primitive function, specific to Undergraduate Students cout << "Report of the Undergraduate Student" << endl; } }; //-------- Subtype: Master Student ---------- class MasterStudent : public Student{ private: // It can also be protected void calculateAverage(){ // Concrete primitive function, specific to Master Students cout << "Average of the Master Student" << endl; } void printReport(){ // Concrete primitive function, specific to Master Students cout << "Report of the Master Student" << endl; } }; // Testing the implentation int main() { UnderGradStudent uStudent; uStudent.createReport(); cout << "------------" << endl; MasterStudent mStudent; mStudent.createReport(); return 0; }