子类和父类具有同名的成员函数:
子类访问父类的函数 子类.父类::成员函数名 来调用父类的成员函数(作用域)
子类一旦发生重载 名为 xxxxx 的函数,那么会屏蔽 父类的所有名为 xxxxx 的函数。
#include<iostream>
#include<cstdio>
using namespace std;
class A{
public:
int _a = 10;
public:
void show(int a){
cout<<"A_show()"<<endl;
}
void func(){
cout<<"A_func()"<<endl;
}
void func(int a){
cout<<"A_func_a"<<endl;
}
};
class B: public A{ // 除了私有内容无法访问 其他内容访问权限不变
public:
void func(){
cout<<"B_func()"<<endl;
}
};
void f(){
B b = B();
b.show(100); // 非同名的可以直接访问
b.func(); // 同名的访问的是子类的
b.A::func(); // 同名的访问如果想访问父类的需要添加作用域
// b.func(100); // 会发现把父类中 func(int a)也屏蔽了
b.A::func(100);
}
int main(){
f();
return 0;
}