I am new to C++. While trying sample polymorphism code, I found that base class virtual function definition in derived class is possible only when defined within the derived class or outside with declaration in derived class.
Following code gives error:
class B
{
public:
virtual void f();
};
void B::f() {
std::cout<<"B::f";
}
class D : public B
{
public:
void f2() {int b;}
};
// error: no "void D::f()" member function declared in class "D"
void D::f() {
std::cout<<"D::F";
}
It works if I declare f() inside D. I was wondering why do I need to explicitly declare the function again when it is already declared in Base class. The compiler can get the signature from Base class right?
Thanks in advance..
"I was wondering why do I need to explicitly declare the function again"
. These are the rules of the gamec++
. – Sigh