zoukankan      html  css  js  c++  java
  • 虚函数的简单应用

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

    //继承多个接口,但是有相同的方法
    #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;
    }
    
  • 相关阅读:
    没提供编码格式,读文件时要怎么推测文件具体的编码
    Spring系列.@EnableRedisHttpSession原理简析
    程序员必备画图技能之——流程图
    痞子衡嵌入式:第一本Git命令教程(7.1)- 清理之缓存(stash)
    痞子衡嵌入式:第一本Git命令教程(6)- 日志(log/reflog/gitk)
    痞子衡嵌入式:第一本Git命令教程(5)- 提交(commit/format-patch/am)
    痞子衡嵌入式:第一本Git命令教程(4)- 转移(add/rm/mv)
    痞子衡嵌入式:第一本Git命令教程(3)- 变动(status/diff)
    痞子衡嵌入式:第一本Git命令教程(2)- 连接(remote/clone)
    痞子衡嵌入式:第一本Git命令教程(1)- 准备(init/config/.gitignore)
  • 原文地址:https://www.cnblogs.com/sven/p/1658411.html
Copyright © 2011-2022 走看看