Andrew No
Andrew No

Reputation: 535

Basic C++ inheritance

In school and in hundreds of online videos, C++ inheritance is taught through a single file; all the classes are declared above main. I've searched far and wide to find a single example of how inheritance works in conjuction with header files, but I am surprised no one has ever asked this before.

How does c++ inheritance work in conjuction with header files? Does each sub class need it's own new header file that extends the base header, or can the subclass definition file define the functions for superclass header file?

Also, does an abstract class affect the above questions at all?

Upvotes: 4

Views: 4022

Answers (4)

Bibek
Bibek

Reputation: 9

Including the Header with base class defined on it must enable the any class within given source file to inherit the base class.

So i think we do not need to include header for each derived class. Since the question asks how the inheritance works in conjuction with header file, here is the simple demonstration

File1: base.h

   class Shape
   {
      public:
         void setWidth(int w)
         {
            width = w;
         }
         void setHeight(int h)
         {
            height = h;
         }
      protected:
         int width;
         int height;
};

File2 : derived.h

#include "base.h"
// Derived class
class Rectangle: public Shape
{
   public:
       int getArea()
       { 
          return (width * height); 
       }
};

File 3: main.cpp

#include"derived.h"
using namespace std;
int main(void)
{
    Rectangle Rect;
    Rect.setWidth(5);
    Rect.setHeight(7);

    // Print the area of the object.
    cout << "Total area: " << Rect.getArea() << endl;
    return 0;
}

Output will be 35. I hope it helps.

Upvotes: 0

eerorika
eerorika

Reputation: 238411

Does each sub class need a it's own new header file that extends the base header

No. Each sub class don't need to be defined in a separate header file. They can all be in one big header too. But it's a good idea to keep them separate, because the user of one subclass might not need the definition of all of them.

If by that extends the base header you mean that they include the header which defines the baseclass, then yes, because the base class must be defined in order to define a class that inherits it.

or can the subclass definition file define the functions for superclass header file?

As long as all non-inline functions are defined in some compilation unit (a .cpp file) and no such function is defined in more than one compilation unit, then all is good. But to avoid confusion, I would recommend defining the functions of the base class in it's own, separate compilation unit.

Also, does an abstract class affect the above questions at all?

No.

Upvotes: 1

user4842163
user4842163

Reputation:

I'll attempt a kind of informal answer -- not so technical, since I think it's right for the level of the question.

Header files aren't magical or anything at all. You could even name them with a ".cpp" extension and #include them. That'd be a horrible and confusing convention, but you can do it. When you do something like:

#include "foo.h"

... all the preprocessor does is grab the contents and foo.h and kind of copies and pastes it into the source file. So there's no special status with respect to header files, and probably the reason no one has asked this kind of question before is because it's somewhat of a strange question.

What is important is with respect to what the compiler sees and what the linker has available when you try to put all this code together. If you are inheriting from a base class (superclass), then you need to ensure that the compiler can see the definition of the base class at the time you are attempting to inherit from it. For example:

class Sub: public Base // the compiler must be able to see the 
                       // definition of 'Base' at this point.

This is what you need to ensure in terms of order. One way to do that is just stick them both in the same header file:

class Base {...};
class Sub: public Base {...};

... of course with the necessary include guards (strongly recommended). Probably easier and generally cleaner in terms of organization is more like this:

// Base.h
#ifndef BASE_H
#define BASE_H

class Base {...};

#endif

// Sub.h
#ifndef SUB_H
#define SUB_H

#include "Base.h"

class Base: public Sub {...};

#endif

Either of these two will ensure that the compiler can see the definition of Base at the time Sub attempts to inherit from it. Last but not least, abstraction makes no difference in this context. All you have to do is ensure that the compiler can see what it needs to see at the right time by providing/including code defining these classes in the right order.

Upvotes: 4

Patrick87
Patrick87

Reputation: 28312

In C++, the content of header files are inserted by the preprocessor where they are #included. Therefore, there is no substantial difference between putting all the definitions you're using in a single file and splitting those definitions among various header files. The same rules apply - in particular, you need to declare the parent class before the child class, so you'll need to #include the relevant parent class before the child class. Abstract classes do not change any of this discussion. Header files are a nicety that the preprocessor gives us humans, not anything that the C++ language understands as such.

Upvotes: 5

Related Questions