覆盖的是指子类函数覆盖基类函数
在不同的类内(分别位于子类和父类)。
同名同参。
基类的函数名前必须有virtual关键字。
隐藏指派生类的函数隐藏了基类的同名函数
如果派生类函数与基类函数同名,但参数不同,无论基类函数前是否有virtual修饰,基类函数被隐.
如果派生类函数与基类函数同名,参数也相同,但是基类函数前无virtual修饰,基类函数被隐藏。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class
Base
{
public
:
virtual
void
f(
float
x){ cout <<
"Base::f(float) "
<< x << endl; }
void
g(
float
x){ cout <<
"Base::g(float) "
<< x << endl; }
void
h(
float
x){ cout <<
"Base::h(float) "
<< x << endl; }
};
class
Derived :
public
Base
{
public
:
virtual
void
f(
float
x){ cout <<
"Derived::f(float) "
<< x << endl; }
void
g(
int
x) { cout <<
"Derived::g(int) "
<< x << endl; }
void
h(
float
x){ cout <<
"Derived::h(float) "
<< x << endl; }
};
|
Derived::f(float x)函数覆盖了基类Base::f(float x)函数。子父类同名同参,父类有virtual关键字
Derived::g(int x) 函数隐藏了基类Base::g(float x)函数。同名不同参,无论有无virtual修饰
Derived::f(float x)函数隐藏了基类Base::f(float x)函数。同名同参,且基类无virtulal修饰