Multiple Classes Per Header

Can I declare multiple classes in one header file? When should I do this?

Yes, you can declare multiple classes in one header file! While the convention is one class per file, there are good reasons to group related classes together.

When to Group Classes

Good reasons to put multiple classes in one header:

  • Small helper classes that are only used by the main class
  • Closely related classes that always work together
  • A set of classes that form a cohesive feature

Here's an example:

// Inventory.h
#pragma once

class Item {
public:
  virtual int GetValue() = 0;
};

class InventorySlot {
  Item* StoredItem{nullptr};
  int Quantity{0};
public:
  void Store(Item* NewItem) {
    StoredItem = NewItem;
    Quantity = 1;
  }
  Item* GetItem() { return StoredItem; }
};

class Inventory {
  static const int MaxSlots{20};
  InventorySlot Slots[MaxSlots];
public:
  bool AddItem(Item* NewItem);
  Item* GetItem(int SlotIndex);
};

When to Separate Classes

You should create separate header files when:

  • Classes are used independently
  • Classes are large or complex
  • Classes might be reused in different contexts
  • Different teams work on different classes

Example of when to separate:

// Weapon.h
#pragma once
class Weapon {
  int Damage{10};
public:
  int GetDamage() { return Damage; }
};
// Character.h
#pragma once
#include "Weapon.h"

class Character {
  Weapon* EquippedWeapon{nullptr};
public:
  void EquipWeapon(Weapon* NewWeapon) {
    EquippedWeapon = NewWeapon;
  }
};

Guidelines

Consider these factors:

  • Will other developers expect to find these classes together?
  • Does it make sense to reuse one class without the others?
  • Would separating the files make the code harder to understand?
  • Is the header file getting too large to manage easily?

Remember, these are guidelines, not strict rules. Use your judgment based on your specific situation.

Header Files

Explore how header files and linkers streamline C++ programming, learning to organize and link our code effectively

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Header File Syntax: <> vs ""
Why do some header files use angle brackets (<>) while others use quotes ("")?
Including Headers in CPP Files
Why do we need to include the header file in its own cpp file? The cpp file already has all the class code!
Circular Dependencies
Can I have circular dependencies if I use pointers? I noticed the lesson showed a Character with a Sword pointer, and a Sword with a Character pointer. How does that work?
Header File Locations
How does the compiler know where to find my header files? What if they are in different folders?
Understanding #pragma once
Why do we need #pragma once? What does it do exactly?
Separating Declarations
Should I always move function definitions to cpp files? The lesson mentioned small functions can stay in the header - how do I decide?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant