ccplusplus.com
Learn C, C++ Concepts
Thursday, January 5, 2012
template method pattern example
/******************************************************************* * File : template-method-pattern.cpp * Author : Saurabh Gupta * Description : template method pattern example * Template method provides an implementation * in a derived class, to be used by the base class. * Date : PM 08:32 05 January 2012 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : *******************************************************************/ #include <string> #include <iostream> using namespace std; class Cccplusplus { public: virtual ~Cccplusplus() { } void output() { m_write("ccplusplus.com"); m_writeendl(); }; virtual void m_write(const std::string &)=0; virtual void m_writeendl()=0; }; class CccplusplusTemplate : public Cccplusplus{ public: void m_write(const std::string & str){ cout << str; } void m_writeendl(){ cout << endl; } }; void display_message(Cccplusplus & ccpp){ ccpp.output(); } int main() { CccplusplusTemplate ccpp; display_message(ccpp); return 0; } /* * OUTPUT * [sgupta@rhel55x86 template-method]$ c++ template-method-pattern.cpp -o template-method-pattern [sgupta@rhel55x86 template-method]$ ./template-method-pattern ccplusplus.com [sgupta@rhel55x86 template-method]$ */
See Also:
Design pattern Concept and Sample Codes
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment