is it necessary to include base class in the header and or .cpp file of derived class?

Dani AI

Generated

Short answer: when a class actually inherits from another, the derived class’s header must see the full definition of the base class at the point the derived class is declared — a forward declaration is not enough for inheritance. A forward declaration is fine only when the header stores or passes pointers/references to the base type and never needs its size or member layout in that translation unit.

The example below shows the forward-declaration pattern (different from inheritance) so that heavy headers are kept out of lightweight headers:

// holder.h
class Heavy;          // forward declaration
class Holder {
    Heavy* h;         // pointer OK with incomplete type
    void doWork();    // definition uses Heavy and lives in .cpp
};

// holder.cpp
#include "holder.h"
#include "heavy.h"       // full definition required for implementation
void Holder::doWork() { /* use Heavy here */ }

Practical points and gotchas:

  • When declaring class Derived : public Base the compiler needs Base’s complete type at that point, so put the base header into the derived header (this is what ’s post was getting at).
  • Each .cpp should include its own header first; then include any other headers needed for definitions. Never include .cpp files. (Note: ’s derived.cpp snippet accidentally showed #include "derived.cpp" — that should be #include "derived.h".)
  • Follow “include what you use”: if the .cpp calls Base members, include Base’s header there as well (even if it is already pulled in transitively).
  • If objects will be deleted polymorphically through a Base pointer, make Base’s destructor virtual.

In short, include the base header in the derived header when inheriting; otherwise prefer forward declarations in headers to reduce coupling and include full headers in implementation files.

Recommended Answers

All 4 Replies

You dont have to have it in your .h file for your derived class but you do need to included it in you .h file.

I was unable to understand, kindly say again NathanOliver.

Use this as an example:
base class

//base.h
#ifndef BASE_H
#define BASE_H

class Base
{
    int foo;

public:
    Base(int number) : foo(number) {}
};

#endif

derived class

//derived.h
#ifndef DERIVED_H  //<- inclusion gaurding
#define DERIVED_H

#include "base.h"  //<- this adds the code from base.h to derived.h

class Derived : public Base
{
    int foobar;
public:
    Derived(int num1, int num2) : Base(num1), foobar(num2) {}
};

#endif

This is how your headers would look. Then in the .cpp files you would just need to include the .h that the .cpp file is named after.

base class .cpp file

//base.cpp
#include "base.h"

// all the code for the base class would go here

derived class .cpp file

//derived.cpp
#include "derived.cpp"

// all the code for the derived class would go here.

Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.