Design pattern-Prototype mode (c + + implementation)

Source: Internet
Author: User

1#include <iostream>2#include <string>3 4 using namespacestd;5 6 classCprototype7 {8  Public:9 Cprototype ()Ten         { One  A         } -Cprototype (intiage): M_iage (iage) -         { the  -         } -         Virtualcprototype* Clone () =0; -  +         intM_iage; - }; +  A classCconcreteprototypea: PublicCprototype at { -  Public: -Cconcreteprototypea (intIage,Const string&strName): Cprototype (iage), M_strname (strName) -         { -  -         } in  - //virtual cprototype* Clone () to         Virtualcconcreteprototypea*Clone () +         { -                 return NewCconcreteprototypea (* This); the         } *  $ Private:Panax NotoginsengCconcreteprototypea (Constcconcreteprototypea&tmp) -         { the                  This->m_iage =Tmp.m_iage; +                  This->m_strname =Tmp.m_strname; A         } the  +  Public: -         stringM_strname; $ }; $  - intMainintargcChar*argv[]) - { the //cprototype* Psttypea = new Cconcreteprototypea ("Lisi"); - //cprototype* psttypeb = Psttypea->clone ();Wuyi  thecconcreteprototypea* Psttypea =NewCconcreteprototypea ( -,"Lisi"); -  Wucconcreteprototypea* PSTTYPEB = psttypea->Clone (); -Psttypeb->m_iage = +; AboutPsttypeb->m_strname ="Zhangsan"; $  -cout<<"Psttypea.m_iage:"<< psttypea->m_iage<<"\tpsttypea.m_strname"<< psttypea->m_strname<<Endl; -cout<<"Psttypeb.m_iage:"<< psttypeb->m_iage<<"\tpsttypeb.m_strname"<< psttypeb->m_strname<<Endl; -  A         DeletePsttypea; +         DeletePsttypeb; the  -         return 0; $ } the //////////////////////////////// the[Email protected] ~/learn_code/design_pattern/7_proto_type]$.ProtoType thePsttypea.m_iage: -Psttypea.m_strname Lisi thePsttypeb.m_iage: +Psttypeb.m_strname Zhangsan

Design pattern-Prototype mode (c + + implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.