继承同名问题

#include<iostream>
using namespace std;

class base
{
public:
    int m_a;
    base()
    {
        m_a = 100;
    }
    void func()
    {
        cout << "base-调用" << endl;
    }
    void func(int a)
    {
        cout << "base-int a 调用" << endl;
    }
};

class son : public base
{
public:
    int m_a;
    son()
    {
        m_a = 200;
    }
    void func()
    {
        cout << "son-调用" << endl;
    }
 };

int main(void)
{
    //同名属性的处理方法
    son a;
    cout << "son 中 m_a = " << a.m_a << endl;
    cout << "base 中 m_a = " << a.base::m_a << endl;
    //同名函数的处理方法
    a.func();
    //如果父类和子类中出现同名的函数,子类的函数会隐藏掉父类中所有的同名函数
    //如果想访问到父类中被隐藏的同名成员函数,需要加作用域
    a.base::func();
    a.base::func(100);
}

 

posted @ 2021-01-10 13:52  loliconsk  阅读(38)  评论(0)    收藏  举报