C++ Namespaces
The access page left one line on the screen in box.cpp: pine, after Tag t("pine"); and t.show() with word under private:. This page stays in the same file and the same Classes module path on disk. The new work groups a free function name inside a namespace so you call it with a scope name and ::. The screen still shows one line: pine.
A namespace is a named scope for names that are not sitting inside one class object. On this page you write namespace Lab with braces, and inside it void mark() that prints pine. Then main calls Lab::mark();. The workshop file stays box.cpp. The value pine is the print from mark, not a private word on a Tag. Reading Lab::mark() is reading a call that needs the namespace name before ::, with no Tag t first.
Programs use namespaces so two libraries can both own a name like mark without colliding in the global scope. You do not keep the full private Tag body as the lesson path on this page. Creating a class inside a namespace can wait for later practice. Here the point is only the namespace block and the Lab:: call. The next page names how you split declarations into header files. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine. Keep #include <iostream> for the print. You do not need #include <string> on this page because there is no std::string word in the checked file.
Above main, write namespace Lab with an opening brace, then void mark() that prints pine with std::cout, then the closing brace for the namespace. Keep #include <iostream> at the top. There is no class Tag in the checked file on this page. There is still no second main inside Lab. The function mark is a free function inside the namespace, not a member of a class.
Inside main, write only Lab::mark(); then return 0;. Do not write Tag t("pine");. Do not write mark(); with no Lab:: as the lesson path. The namespace name before :: is required for this call shape.
Walk the run carefully from the start. The program starts at main. Lab::mark(); finds mark inside namespace Lab and runs its body. The body prints pine with std::cout and ends the line. No class object was built, and no private member was filled. Then mark finishes, main returns, and the program ends. There is one line on the screen. It is pine. The access-shaped Tag body from the last page is gone from the checked file so this namespace call stays clear. If you still see a constructor or t.show, the old body was not replaced yet.
If you write mark(); with no Lab:: in front, the build fails because mark is not a free global name in main on this page. That failed experiment stays in the prose. The checked file always writes Lab::mark();.
If you forget the closing brace of the namespace before main, the build fails about braces. The checked file always closes Lab before int main().
A common mistake is to write Lab.mark() with a dot like some other languages; C++ uses Lab::mark(). Another common mistake is to put main inside the namespace by accident. A third mistake is to leave the old Tag class and never add namespace Lab. A fourth mistake is to call Tag::mark(); from the static page as if it were still the lesson. A fifth mistake is to drop #include <iostream> after removing the string include.
This page stops before the next tool, where you move declarations into a header file that other files can include. The next page names that tool once at the end. The Lab namespace you have just written only shows a named scope around a free function. Keep the block small on purpose: one namespace name, one function body, one call from main, and exactly one line on the screen.
Open box.cpp. Keep #include <iostream>. Replace the private Tag body with namespace Lab holding void mark() that prints pine. In main, write Lab::mark(); and remove the object creation lines. The file name on disk stays box.cpp. The checked file is below, and the new lines are the namespace block and the Lab::mark(); call.
#include <iostream>
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. 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. The table gives each row its piece, so compare your run with it line by line. If the screen is blank, mark may not print. If the build fails about mark with no Lab::, add the namespace name and ::. If the build fails about braces, close the namespace before main.
📊 namespace and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | pine, because Lab::mark runs and prints pine |
| Last | pine, because the program prints one line and stops |
| Rule | a namespace groups names so you call them with Name:: without a class object |
| Screen | pine |
Try one change at a time. Change the print inside mark from "pine" to "oak", leave the call as Lab::mark();, and rebuild. The screen shows oak instead of pine, because only the print text changed inside the namespace function. Put "pine" back. Next, temporarily write a bare mark(); in main and rebuild to see the missing-scope error, then restore Lab::mark(); so the file returns to the checked shape. Each experiment changes one thing on purpose, so the screen or the compiler shows exactly whether the namespace name was required. Undo each change before the next one starts, so the file always returns to the checked version with one clear Lab:: call.
This page ends the namespaces tool and stops before the next one. The next page is headers, which splits declarations into a file other sources can include. The Lab namespace you have just written only shows grouping a free function under a named scope. 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.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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