C++ Access
The static page left one line on the screen in box.cpp: pine, after Tag::mark(); with no object. This page stays in the same file and the same Classes module. The type name Tag stays. The new work chooses who outside the class can name a member directly. The screen still shows one line: pine.
Access labels sit inside the class before groups of members. On this page you write private: above std::string word; and public: above the constructor and show. Then main builds with Tag t("pine"); and calls t.show();. The Classes cast keeps Tag and box.cpp. The value pine lives in the private member; show can read it because show is inside Tag. Reading t.word from main is blocked on purpose. That blocked name is the point of this page.
Programs hide data so callers use clear public tools instead of touching every member. You do not leave word under public: on this page. Callers should ask show to print, not reach into the object field by name from main. Creating two objects still works, but this page builds one object so the screen stays one clear line. The next page names a tool that groups names into a named scope. Here the point is only private for data and public for the tools main may call after the object exists. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine. Keep #include <string> again because std::string word returns on this page.
Above main, write class Tag with private: then std::string word;. Under public: write the constructor Tag(std::string word) that sets this->word = word;, and void show() that prints word. Keep #include <iostream> and #include <string>. There is still no second main inside Tag. The static mark helper from the last page is gone from the checked file so access stays clear.
Inside main, write Tag t("pine");, then t.show();, then return 0;. Do not write t.word = "pine"; from main. Do not print t.word from main. The public constructor and show are the allowed path.
Walk the run. The program starts at main. Tag t("pine"); builds one object and runs the constructor, which sets the private member to pine from inside the class. t.show(); enters show inside Tag, reads word without any problem, and prints pine. Then show finishes, main returns, and the program ends. There is one line on the screen. It is pine. If you try std::cout << t.word from main while word is private, the build fails about access. That failed line stays in the prose only, never in the checked file.
If you write t.word = "pine"; from main while word is under private:, the build fails about access. That failed experiment stays in the prose. The checked file always fills word inside the constructor.
If you move word back under public: and keep everything else, t.word from main would compile again, but this page wants the private shape. The checked file keeps private: above word.
A common mistake is to put the constructor under private: so Tag t("pine"); fails. Another common mistake is to put show under private: so t.show(); fails. A third mistake is to leave every member public and never try the private label. A fourth mistake is to print t.word from main as the lesson path. A fifth mistake is to drop #include <string> after bringing word back.
This page stops before the next tool, where names live inside a named scope outside a single class. The next page names that tool once at the end. The private word you have just written only shows how access labels hide data from main while public tools still work. Keep the labels in a clear order on purpose: private data members first, then the public tools that main is allowed to call.
Open box.cpp. Keep the iostream and string includes. Replace the static-only body with private: std::string word;, then public: constructor and show. In main, write Tag t("pine"); and t.show();. The file name on disk stays box.cpp. The checked file is below, and the new lines are the private: label and the public tools around it.
#include <iostream>
#include <string>
class Tag
{
private:
std::string word;
public:
Tag(std::string word)
{
this->word = word;
}
void show()
{
std::cout << word << std::endl;
}
};
int main()
{
Tag t("pine");
t.show();
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. The constructor stored pine in the private member, and t.show printed it. The table gives each row its piece, so compare your run with it line by line. If the screen is blank, show may be missing. If the build fails about t.word, you tried to use the private member from main. If the build fails about show being private, move show under public:.
📊 access and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | pine, because the constructor fills private word and show prints it |
| Last | pine, because the program prints one line and stops |
| Rule | private hides a member from main; public tools on the class can still use it |
| Screen | pine |
Try one change at a time. Change Tag t("pine"); to Tag t("oak");, leave show as it is, and rebuild. The screen shows oak instead of pine, because only the constructor argument changed and the private member still held that value for show. Put "pine" back. Next, temporarily add std::cout << t.word << std::endl; in main and rebuild to see the access error, then remove that line so the file returns to the checked shape. Each experiment changes one thing on purpose, so you see either a new printed word on the screen or a clear access failure from the compiler. Undo each change before the next one starts, so the file always returns to the checked version with one public call path only.
This page ends the access tool and stops before the next one. The next page is namespaces, which groups names into a named scope. The private word you have just written only shows hiding data from main while show still prints through the public path. 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.
🧠 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