geeksforgeeks 011-函数重载在继承中是否有效

在父类中有一个函数,在其派生类中有一个函数名相同的函数,那么派生类的对象无法调用父类的函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
class Base
{
public:
int (int i)
{

cout << "f(int): ";
return i+3;
}
};
class Derived : public Base
{
public:
double (double d)
{

cout << "f(double): ";
return d+3.3;
}
};
int main()
{
Derived* dp = new Derived;
cout << dp->f(3) << 'n';
cout << dp->f(3.3) << 'n';
delete dp;
return 0;
}

The output of this program is:

1
2
f(double): 6.3
f(double): 6.6

Instead of the supposed output:

1
2
f(int): 6
f(double): 6.6

在父类和子类中没有函数重载