• 虚函数的简单应用


      代码只是示例了虚函数的一些应用,并不是说在该场景下这样用最好,甚至于说,几乎不会出现这样的用法。但这可以让初学者加深对虚函数的理解。

    //继承多个接口,但是有相同的方法
    #include <iostream>
    
    class IHello
    {
    public:
      virtual void Say()=0;
    };
    
    class IWorld
    {
    public:
      virtual void Say()=0;
    };
    
    class MHello:public IHello
    {
    public:
      void Say(){cout<<"Hello";}
    };
    
    class MWorld:public IWorld
    {
    public:
      void Say(){cout<<"World"<<endl;}
    };
    
    class CObject:public MHello,public MWorld
    {
    
    };
    
    int main()
    {
      CObject 0;
      IHello& ih=0;
      IWorld& iw=0;
      ih.Say();
      iw.Say();
      return 0;
    }
    
    
    
    //两个平行类,通过被继承,虚函数,将下一个动作传到另一个类,V形转发
    #include <iostream>
    using namespace std;
    
    class Hello
    {
    public:
      void Say(){cout<<"Hello";nextH();}
      virtual void nextH(){}
    };
    
    class World
    {
    public:
      void Say(){cout<<"World";nextW();}
      virtual void nextW(){}
    };
    
    class CObject:public Hello,public World
    {
    public:
      void nextH()
      {
        World::Say();
      }
      void nextW()
      {
        ;
      }
    };
    
    int main()
    {
      CObject o;
      o.Hello::Say();
      return 0;
    }
    
    //受到上面平行类的启发,利用菱形继承,提供数据,实现一个switch,处理类似平行的
    //但是对每个平行类要有不同签名的调用下一个的虚函数,并且在CObject中对这个处理过程进行编码
    #include <iostream>
    using namespace std;
    
    class Data
    {
    public:
      int value;
    };
    
    class Case1:public virtual Data
    {
    public:
      void process(){if(value==1){cout<<1<<endl;} else next1();}
      virtual void next1(){}
    };
    
    class Case2:public virtual Data
    {
    public:
      void process(){if(value==2){cout<<2<<endl;} else next2();}
      virtual void next2(){}
    };
    
    class Default:public virtual Data
    {
    public:
      void process(){cout<<"default"<<endl;}
    };
    
    class CObject:public Case1,public Case2,public Default
    {
    public:
      void next1()
      {
        Case2::process();
      }
      void next2()
      {
        Default::process();
      }
    };
    
    int main()
    {
      CObject 0;
      for(int i=0;i<4;++i)
      {
        o.value=i;
        o.Case1::process();
      }
      return 0;
    }
    

    //想到责任链模式,也实现了一个 //但是一个处理者知道下一个处理者 #include <iostream> using namespace std; class MsgProcessor { public: MsgProcessor(MsgProcessor* next=NULL):next_(next){} virtual void process(int msg)=0; virtual void dispatch(int msg) { if(next_) next_->process(msg); } MsgProcessor* next_; }; class Handler1:public MsgProcessor { public: Handler1(MsgProcessor* next=NULL):MsgProcessor(next) { } void process(int msg) { if(msg==1) { cout<<1<<endl; } else { dispatch(msg); } } }; class Handler2:public MsgProcessor { public: Handler2(MsgProcessor* next=NULL):MsgProcessor(next) { } void process(int msg) { if(msg==2) { cout<<2<<endl; } else { dispatch(msg); } } }; class HandlerDefault:public MsgProcessor { public: HandlerDefault(MsgProcessor* next=NULL):MsgProcessor(next) { } void process(int msg) { cout<<"default"<<endl; } }; int main() { HandlerDefault dft; Handler2 h2(&dft); Handler1 h1(&h2); for(int i=0;i<4;i++) { h1.process(i); } return 0; }

    //修饰器模式,好看一点,把终止结点和非终止节点分开了
    #include <iostream>
    using namespace std;
    
    class Component
    {
    public:
      virtual void process(int msg)=0;
    };
    
    class CaseDefault:public Component
    {
    public:
      void process(int msg)
      {
        cout<<"default"<<endl;
      }
    };
    
    class CaseNormal:public Component
    {
    public:
      CaseNormal(Component* next=NULL):next_(next)
      {
      }
      virtual void process(int msg)
      {
        if(next_) next_->process(msg);
      }
      Component* next_;
    };
    
    class Case1:public CaseNormal
    {
    public:
      Case1(Component* next=NULL):CaseNormal(next)
      {
      }
      void process(int msg)
      {
        if(msg==1)
        {
          cout<<1<<endl;
        }
        else
        {
          CaseNormal::process(msg);
        }
      }
    };
    
    class Case2:public CaseNormal
    {
    public:
      Case2(Component* next=NULL):CaseNormal(next)
      {
      }
      void process(int msg)
      {
        if(msg==2)
        {
          cout<<2<<endl;
        }
        else
        {
          CaseNormal::process(msg);
        }
      }
    };
    
    int main()
    {
      CaseDefault dft;
      Case2 c2(&dft);
      Case1 c1(&c2);
      for(int i=0;i<4;++i)
      {
        c1.process(i);
      }
      return 0;
    }
    
  • 相关阅读:
    关于使用gitlab协同开发提交代码步骤
    一些JavaScript中原理的简单实现
    关于JavaScript中bind、applay、call的区别
    在腾讯云centos7.2上安装配置Node.js记录
    JAVA Web期末项目第三阶段成果
    在腾讯云服务器上安装JDK+Tomcat并启动tomcat
    《JavaScript算法》二分查找的思路与代码实现
    将本地的一个项目托管到自己的GitHub仓库
    《JavaScript算法》常见排序算法思路与代码实现
    项目经理建议:管理时间等于管理自己
  • 原文地址:https://www.cnblogs.com/sven/p/1658411.html
Copyright © 2020-2023  润新知