Dashboard/Block 3: Data Structures & OOP/Week 6
Week 6Data Structures & OOP

Header Files

Alright, let's be real — up until now, you've probably been putting all your code in one file. And that works fine when your program is small. But imagine scrolling through a 5,000-line file trying to find one function. Yeah, not fun. This week, you'll learn how the pros do it: by splitting code into header files (.h) and source files (.cpp). And this isn't just about keeping things tidy — it's actually fundamental to how C++ compilation works, and it's what lets entire teams work on different parts of the same project without stepping on each other's toes.

The idea is actually pretty simple: header files contain declarations — they tell the compiler what exists (class definitions, function prototypes). Source files contain implementations — the actual code that does the work. When another file needs to use your class, it just #includes the header to learn what's available, without needing to see how everything works under the hood. Think of it like this: the header is the table of contents of a book, and the source file is the actual chapters.

We'll also cover include guards — this is a must-know. Without them, if two files both #include the same header, the compiler sees duplicate definitions and freaks out with errors. Include guards use #ifndef, #define, and #endif to make sure each header's contents only get included once, no matter how many files try to include it.

File TypeExtensionContainsPurpose
Header file.h or .hppClass declarations, function prototypesDefine the interface — what exists
Source file.cppMethod definitions, function bodiesProvide the implementation — how it works
Main filemain.cppint main() and program logicEntry point — where execution begins

Once you get the hang of this, you'll never want to go back to single-file programs. This is how every real-world C++ project works, from small assignments to massive codebases with thousands of files.

Learning Objectives

Split classes into .h (declaration) and .cpp (definition)
Use include guards (#ifndef)
Understand the compilation and linking process
Organize a multi-file C++ project

Key Concepts

Header Files and Include Guards