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

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

#ifndef BASE_H
#define BASE_H

class Base
    int foo;

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


derived class

#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;
    Derived(int num1, int num2) : Base(num1), foobar(num2) {}


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

#include "base.h"

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

derived class .cpp file

#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, learning, and sharing knowledge.