Title pretty much says it all- do Derived classes have to re-include the definitions of functions/data members inherited from Parent classes in the header file?
Can the inherited functions/members be implied from the inheritance statement? Should they be explicitly re-declared for readability or could this risk hiding?
I am unsure what I should do in header files of derived classes.
Does C++ derived class ever have to include definitions of inherited functions/members in header file?
Taken very literally: no - in C++, the definition of any non-template function - irrespective of inheritance or class membership - need never appear in a header. Classes only define static data members - in their implementation file per the One Definition Rule - and the base-class definition needn't and can't be repeated in any derived class.
That said, I think you must be thinking of function declarations, which:
using
statement to expose hidden base-class functionsvirtual
function definition/implementation (with very limited variation allowed for a co-variant return type, and the ability to change default parameters - but don't do it!).For example:
struct Base { virtual void f() = 0; };
struct Derived : Base { virtual void f(); }; // must mention f() here...
// then either in same header as Derived (and `inline` per One Definition Rule) or
// out-of-line in an implementation file...
?inline? void Derived::f() { std::cout << "Hello world\n"; } // ...to define it here
If you don't mention f()
inside struct Derived { }
, the compiler will assume you're going to:
= 0
for a pure virtual function so none's available)), orCan the inherited functions/members be implied from the inheritance statement? Should they be explicitly re-declared for readability or could this risk hiding?
For almost all cases this is covered by what I've said above (i.e. you only re-declare functions you'll provide new definitions for); the exception is for functions that are pure virtual in the base class and will remain pure virtual in your derived class... you can optionally declare them if you think it has some documentation value - it makes no difference at all.
I am unsure what I should do in header files of derived classes.
Normally you just want to specify:
using
statements for any hidden functions you wish to exposevirtual
functions that you'll provide new definitions for, whether inline in the derived class's header or out-of-line in its implementation fileYou don't need to repeat data member definitions.
Does C++ derived class ever have to include definitions of inherited functions/members in header file?
You need to specify the parent (base) class declaration via a header file. Inherited functions need not be redeclared.
Can the inherited functions/members be implied from the inheritance statement?
If I understand your question correctly, yes they are implied in the sense they need not be explicitly specified again.
Should they be explicitly re-declared for readability
As you're inheriting classes and not functions, re-declaration of functions is not required. Also, re-declaration may not always improve readability (it can clutter the file as well).
or could this risk hiding?
Information Hiding or Abstraction will not be affected as long as the access modifiers are not changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With