当父类中有多个重载的函数,如果子类以任意一种方式重写了父类的函数,那么所有父类的此名字函数都会被隐藏
#include<iostream>
#include<string>
using namespace std;
class Base {
public:
int f() const {
cout << "Base::f() \n";
return 1;
}
int f(string c) const
{
cout << "Base::f(string) \n";
return 1;
}
};
class Derived1 : public Base
{
public:
};
//重写隐藏另外一个函数
class Derived2 : public Base
{
public:
int f() const {
cout << "Derived2::f() \n";
return 1;
}
};
//返回值不同,隐藏父类两个函数
class Derived3 : public Base
{
public:
void f() const {
cout << "Derived3::f() \n";
}
};
//参数不同,隐藏父类两个函数
class Derived4 : public Base
{
public:
int f(int) const {
cout << "Derived4::f() \n";
return 1;
}
};
int main()
{
Base b;
Derived1 d1;
Derived2 d2;
Derived3 d3;
Derived4 d4;
b.f();
b.f("c");
d1.f();
d1.f("c");
d2.f();
// d2.f("c"); //hide
d3.f();
//d3.f("c"); //hide
d4.f(3);
//d4.f("c"); //hide
}