C++继承中的同名成员处理

#include<iostream>
using namespace std;
class Base {
public:
    Base() {
        m_A = 100;
    }
    void func() {
        cout << "Base func()调用" << endl;
    }
    void func(int a) {
        cout << "Base func(int a)调用" << endl;
    }
    int m_A;
};
class Son :public Base {
public:
    Son() {
        m_A = 200;
    }
    void func() {
        cout << "Son func()调用" << endl;
    }
    int m_A;
};
void test01() {
    Son s;
    cout << "Son m_A = " << s.m_A << endl;
    cout << "Base m_A = " << s.Base::m_A << endl;
    s.func();
    s.Base::func(); 
    //s.func(100);报错,子类的同名成员函数隐藏父类的所有同名成员函数,包括重载版本
    s.Base::func(100);
}
int main() {
    test01();
    system("pause");
    return 0;
}

 

posted on 2020-03-13 20:06  ~明月几时有  阅读(139)  评论(0)    收藏  举报