1. 程式人生 > >C++重載、覆蓋、隱藏的區別與舉例

C++重載、覆蓋、隱藏的區別與舉例

重載、覆蓋、隱藏

參考博客:http://blog.csdn.net/hexi_2000/article/details/4392107

//重載,覆蓋,隱藏舉例
#include <iostream>

using namespace std;

class A
{
public:
    int n;
    void fun()
    {
        cout<<"A::fun()"<<endl;
    }
    virtual void fun(int a)
    {
        cout<<"A::fun(int)"<<endl;
    }
    void fun(double f)
    {
        cout<<"A::fun(double)"<<endl;
    }
};

class B:public A
{
public:
    int n;
    void fun(int a)
    {
        cout<<"B::fun(int)"<<endl;
    }
    void fun(double f)
    {
        cout<<"B::fun(double)"<<endl;
    }
};

int main(int argc, char *argv[])
{
    A a;
    B b;
    A *p;
    
    p=&b;
    p->fun();
    p->fun(20);
    p->fun(19.9);
    
    b.fun(19.9);
    b.n=5;
    cout<<b.A::n<<endl;
    
    return 0;
}

技術分享

本文出自 “10628473” 博客,請務必保留此出處http://10638473.blog.51cto.com/10628473/1964423

C++重載、覆蓋、隱藏的區別與舉例