Programming foundations

Stage 1 · Lesson 7 of 8

Header files and multiple source files

Your first language

Once a program grows, split it into several files. Declarations, which say that a exists, go in a such as budget.h. Definitions, the actual code, go in a source file such as budget.cpp.

Then main.cpp includes the header to use them. Each .cpp file is compiled on its own, then joins the results, so g++ main.cpp budget.cpp -o app builds both. Protect every header with #pragma once or , so it is not pasted in twice.

Put function definitions in the .cpp file, not the header. Otherwise you get "multiple definition" errors when two files include it, unless the function is marked inline. Never write using namespace std; in a header, because it leaks into every file that includes it. CMake automates all of this in the projects stage.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with

  • CourseFree
    LearnCpp.com

    LearnCpp.com · Beginner

    Free, current and careful: chapters 0 to 16 take you from setup to references and std::vector

Practise

  • Small exercises in your language track, with free feedback from a human mentor

Keep handy

  • The standard library reference: check std::vector and std::string pages as you use them

  • Quick syntax lookups and an online compiler for testing a snippet on a shared computer

Mark this stage as not done?