In my project, I want implement a child class which inherits from parent class and overload a member function in the parent class which has been overloaded in the parent class. The condition I meet can be summarized in the following code.
#include <iostream>
#include <string>
usingnamespace std;
class parent {
public:
parent(){}
~parent(){}
virtualvoid func(int n) {cout<<"parent::func(int)"<<endl;}
void func(string c) {cout<<"parent::func(string)"<<endl;}
};
class child : public parent {
public:
child(){}
~child(){}
void func(int n) {cout<<"child::func(int)"<<endl;}
};
int main()
{
child ch;
string str;
ch.func(str);
}
The code can't be compiled correctly. The compiler complained there is no function "child::func(string)". I wonder whether I have to copy the code of parent::func(string) to the child class in this condition. Thanks for replies.
class child : public parent {
public:
child(){}
~child(){}
void func(int n) {cout<<"child::func(int)"<<endl;}
// this line unhides it:
using parent::func;
};
I don't understand the rationale of why C++ does it that way. Seems rather silly to me.