I’m trying to understand when it is required to declare a function in a header file when inheriting methods from a parent class.
For example lets say I have the following class:
class parent{
public:
virtual void foo()= 0;
}
Lets say I have a child class that inherits from parent do I have to also declare foo in child’s header file or can I simply define the method in the source file for child?
Would the following delaration be incorrect?
Headerfile:
class child : public parent{
}
Classfile:
child::foo(){
// do something
}
You do not have to declare
foo()in the child’s class definition or its header. It is inherited automatically – that’s what inheritance means. (Of course, the function would have to bepublicrather thanprivatein the base class (parent), but that’s a minor peccadillo.)You simply define the method in the source file for the child class.