zoukankan      html  css  js  c++  java
  • C++覆盖和隐藏

    #include<iostream>
    using namespace std;
    class Base
    {
    public:
     virtual void xfn(int i)
     {
      cout<<"Base::xfn(int i)"<<endl;
     }

     void yfn(float f)
     {
      cout<<"Base::yfn(float f)"<<endl;
     }

     void zfn()
     {
      cout<<"Base::zfn()"<<endl;
     }
    };
    class Derived:public Base
    {
    public:
     void xfn(int i)
     {
      cout<<"Derived::xfn(int i)"<<endl;
     }
     void yfn(int c)
     {
      cout<<"Derived::yfn(int n)"<<endl;
     }
     void zfn()
     {
      cout<<"Derived::zfn()"<<endl;
     }
    };
    void main()
    {
     Derived d;
     Base *pB=&d;
     Derived *pD=&d;
     
     pB->xfn (5);
     pD->xfn(5);
     
     pB->yfn(3.14f);
     pD->yfn(3.14f);
     
     pB->zfn();
     pD->zfn();
    }

    函数的覆盖是发生在派生类和基类之间,两个函数必须完全相同,并且都是虚函数。那么不属于这种情况的就是隐藏了

    运行结果:

    Derived::xfn(int i)
    Derived::xfn(int i)
    Base::yfn(float f)
    Derived::yfn(int n)
    Base::zfn()
    Derived::zfn()
    Press any key to continue

  • 相关阅读:
    js 和 jquery的宽高
    client、offset、scroll
    web开发中会话跟踪的方法有哪些
    前端需要注意哪些SEO
    ES6 Set和Map数据结构
    ES6实现数组去重
    ES6 Symbol
    ES6对象的拓展
    ES6数组的拓展
    ES6函数的拓展
  • 原文地址:https://www.cnblogs.com/feng801/p/1365392.html
Copyright © 2011-2022 走看看