Template mode (C ++)

Source: Internet
Author: User
#include <iostream>using namespace std;class Road{public:    Road(){}    virtual ~Road(){}    void operation()    {        start();        step1();        step2();        step3();        end();    }    void start(){cout<<"start"<<endl;}    void end(){cout<<"end"<<endl;}    virtual void step1()=0;    virtual void step2()=0;    virtual void step3()=0;};class way1 : public Road{public:    way1(){}    virtual ~way1(){}    void step1(){cout<<"way1_step1"<<endl;}    void step2(){cout<<"way1_step2"<<endl;}    void step3(){cout<<"way1_step3"<<endl;}};class way2 : public Road{public:    way2(){}    virtual ~way2(){}    void step1(){cout<<"way2_step1"<<endl;}    void step2(){cout<<"way2_step2"<<endl;}    void step3(){cout<<"way2_step3"<<endl;}};int main(){    Road *pr1=new way1;    Road *pr2=new way2;    pr1->operation();    pr2->operation();    delete pr2;    delete pr1;    system("pause");    return 0;}
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.