Loading...

C++ Headers

The namespaces page left one line on the screen in box.cpp: pine, after Lab::mark(); with the whole mark body in the same file. This page stays in the Classes module path on disk. The new work splits the declaration into a header file named lab.h and keeps the definition plus main in box.cpp. The screen still shows one line: pine.
A header holds declarations that other source files can include. On this page lab.h declares void mark(); inside namespace Lab. Then box.cpp writes #include "lab.h", defines mark, and calls Lab::mark();. The workshop still builds one program from box.cpp. The value pine is still the print from mark. Reading the include line is reading how the declaration arrives in the cpp file before you define and call the function.
Programs use headers so many cpp files can share one declaration without copying it by hand. You do not keep the entire namespace body only in box.cpp as the lesson path on this page. Creating a second cpp file can wait for a later practice day. Here the point is only one header file, one quoted include, one function definition, and one call from main. The next page leaves Classes and names references in the Memory and Collections module. The build command stays familiar: compile box.cpp into box, then run that program. Keep #include <iostream> in the cpp file for the print. The quoted include #include "lab.h" pulls in your header from the same folder before the compiler reaches main.

Create a new file named lab.h next to box.cpp in the same folder. Inside it, open namespace Lab, declare void mark(); with a semicolon and no function body, then close the namespace. A short include guard is fine on this page: #ifndef LAB_H, #define LAB_H, your namespace block, then #endif. The header has no main and no std::cout print body on this page.
In box.cpp, keep #include <iostream>, add #include "lab.h", then define void Lab::mark() or open namespace Lab again and write the body that prints pine. Inside main, call Lab::mark(); and return 0. Do not put the full definition only in the header as the lesson path. Do not call mark(); with no Lab::.
Walk the run after both files exist in the same folder. The compiler reads box.cpp, pulls in the declaration from lab.h, compiles the definition of mark, and builds main. When you run ./box, Lab::mark(); prints pine. No class object is required for this call. There is one line on the screen. It is pine. If lab.h is missing from the folder, the include fails right away. If the declaration and the definition disagree on the name or the namespace, the build fails before you ever see pine.

If you write #include <lab.h> with angle brackets instead of quotes for your own file in this folder, the compiler may not find it the same way. That failed experiment stays in the prose. The checked path uses #include "lab.h".
If you forget to define mark in box.cpp and only declare it in lab.h, the link step fails about an undefined reference. The checked files always define mark in box.cpp.
A common mistake is to put main inside lab.h. Another common mistake is to omit the semicolon on the declaration void mark();. A third mistake is to leave the old single-file namespace body and never create lab.h. A fourth mistake is to compile lab.h alone as if it were a cpp file. A fifth mistake is to drop #include "lab.h" from box.cpp while still calling Lab::mark();.
This page stops before the next tool, where a name can refer to another variable. The next page names that tool once at the end and starts the Memory and Collections module. The header you have just written only shows a declaration file that box.cpp includes. Keep both files in the same folder when you build. Create lab.h first with the include guard and the Lab::mark declaration. The checked header is below.

lab.h declares Lab::mark for other files to include

cpp
#ifndef LAB_H
#define LAB_H

namespace Lab
{
    void mark();
}

#endif

Open box.cpp next in that same folder. Include iostream and lab.h, define mark so it prints pine, and call Lab::mark(); from main. The cpp file name on disk stays box.cpp for this workshop. The checked cpp file is below.

box.cpp includes lab.h, defines mark, calls Lab::mark

cpp
#include <iostream>
#include "lab.h"

namespace Lab
{
    void mark()
    {
        std::cout << "pine" << std::endl;
    }
}

int main()
{
    Lab::mark();
    return 0;
}

Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box. You compile the cpp file; the header is included automatically. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows one line: pine.
Check that line first. Lab::mark printed pine through the declaration in lab.h and the definition in box.cpp. The table gives each row its piece, so compare your run with it line by line. If the build fails about lab.h, check the quoted include and the file name. If the link fails about mark, the definition is missing from box.cpp.

📊 header and what the screen shows

Piece What the screen shows
First pine, because Lab::mark runs after lab.h was included
Last pine, because the program prints one line and stops
Rule a header holds declarations that a cpp file includes before defining and calling them
Screen pine

Try one change at a time. Change the print inside mark in box.cpp from "pine" to "oak", leave lab.h as it is, and rebuild. The screen shows oak instead of pine, because only the definition body changed. Put "pine" back. Next, temporarily rename the include to a wrong file name and rebuild to see the missing-header error, then restore #include "lab.h". Each experiment changes one thing on purpose, so the compiler or the screen shows exactly whether the header path and the definition still match. Undo each change before the next one starts, so both files always return to the checked version with a matching declaration and definition.
This page ends the headers tool and the Classes module path for now. The next page is references, which lets one name refer to another variable. The lab.h declaration you have just written only shows sharing one clear function name through an include into box.cpp. You leave with one line on the screen: pine. Count the lines before you leave. There is exactly one printed line on that final screen, and that single printed line on the screen is still just pine alone right there now after the build.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top