Source Files and Header Files

As programs grow larger and more complex, organization becomes just as important as correctness. In this section, you’ll learn how C++ projects are structured in the real world using source files and header files.

Source Files and Header Files – The Building Blocks of Your Code

Think of a C++ program like a house.

  • The source files (.cpp) are the rooms - you live in them, decorate them, and put things in them. This is where the action happens: the code that actually runs.
    • Contain executable code
    • Hold function definitions and logic
    • This is where behavior is implemented
  • The header files (.h) are the blueprints - they describe what the house will have (functions, classes, constants) without actually building anything yet.
    • Declare functions, classes, and constants
    • Describe what exists without showing how it works
    • Act as a contract between files

Why bother separating them?

  • It keeps your code neat and easy to find.
  • It allows you to share only the “blueprint” with other developers without disclosing every detail of how you built it.
  • It makes big projects faster to compile and easier to maintain.
  • This is the same structure used in professional C++ projects, and building this habit now will make larger data structure projects much easier to manage.

The main() Function – The Entry Point

Every C++ program needs a starting point, and main() is it. When you run your program, the computer says, “Where do I start?” and main() is the door it walks through. In multi-file programs, main() typically lives in a single .cpp file and coordinates work done by functions and classes defined elsewhere.

Here’s the simplest version:

The return 0; part is like telling your operating system, “I’m done, and everything worked fine.” If something goes wrong, you can return a nonzero number - kind of like leaving a note saying, “Hey, there was a problem.”

#include Directives and Namespaces

C++ programs often need extra tools. That’s where #include comes in - it’s like sending out invitations to the code you want to use.

  • #include <iostream> brings in the standard input/output features, like cout for printing.
  • #include "MyHeader.h" brings in your own custom blueprints.

And what’s this namespace thing? Imagine two people in a room named Alex - confusing, right? Namespaces help keep names organized so your program knows which Alex you mean. The C++ Standard Library lives in the std namespace. You can either write:

Rule of Thumb

  • Use < > for standard library headers
  • Use " " for your own project headers
  • Headers describe what exists; source files contain the working code

Namespaces help organize code and prevent name conflicts. The standard C++ library is stored inside the std namespace.
Example:

Without using namespace std;, you’d need to write:

Splitting Interface and Implementation – The Secret to Professional Code

In C++, we like to separate what something does from how it does it.

  • Header file (.h) → the promise (function names, class definitions, constants)
  • Source file (.cpp) → the delivery (the actual code)

Example:
MyFunctions.h

MyFunctions.cpp

You’ll see this same separation pattern again when working with classes, templates, and larger data structures later in the course. This separation makes it easier for multiple developers to work on a project without overwriting each other’s work and improves maintainability.

Pro Tip: Even when a program feels “small,” practicing this separation builds habits that scale smoothly as your projects grow in size and complexity.

Complete and Continue