zoukankan      html  css  js  c++  java
  • 多态

    #include <iostream>
    using namespace std;
    /*
    ""与<>的区别,搜索的开始位置不一样
    */
    class Anmial
    {
    public:
     Anmial()
     {
      cout<<"Anmial construct"<<endl;
     }
     Anmial(int a,int b)
     {
      cout<<a<<endl<<b<<endl;
     }
     void eat()
     {
      cout<<"Anmial eat"<<endl;
     }
     void sleep()
     {
      cout<<"Anmial sleep"<<endl;
     }
     /*
     多态的应用
     可以把virtual清除看看结果
     在c++进行编译时,发现有virtual的函数时候就会采用late binding技术,
     在运行时依据对象的类型来确定调用哪个函数,这就是C++的多态性
     */
     virtual void breath()
     {
      cout<<"Anmial breath"<<endl;
     }
    };
    class fish:public Anmial
    {
    public :
     fish():Anmial(3,3)
     {
      cout<<"fish construct"<<endl;
     }
     void breath()
     {
      cout<<"fish breath"<<endl;
     }
    };
    /*
    引用与指针
    引用是变量的别名,它必须在定义时赋植,不占用内存地址
    指针占用内存。

    引用通常用在值的传递,可以避免值的复制,因为他们是同一块内存,
    指针也可以避免值的复制,而不用指针传递值的原因是因为,指针容易造成语意的混淆
    */
    void brh(Anmial &a)
    {
     a.breath();
    };
    void main()
    {
     fish f;
     brh(f);
    }

  • 相关阅读:
    2019.8.16
    一种抠环的办法
    [HAOI2015]树上染色
    有关树形背包
    2019.7.27
    有关矩阵快速幂
    2019.7.25
    欧拉函数(转载)
    2019.7.22
    phpstudy集成环境安装redis扩展
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/572666.html
Copyright © 2011-2022 走看看