C++ static
The member-functions page left one line on the screen in box.cpp: pine, after Tag t("cone");, t.set("pine");, and t.show(). This page stays in the same file and the same Classes module. The type name Tag stays. The new work is a member function that belongs to the type name itself, so you call it without building an object first. The screen still shows one line: pine.
A static member function belongs to the class, not to one object variable. On this page you write static void mark() inside Tag, and the body prints pine. Then main calls Tag::mark(); with the class name and ::, not with a variable and a dot. The Classes cast keeps Tag and box.cpp. The value pine is the print from mark, not a word member filled by set. Reading Tag::mark() is reading a call that needs no Tag t first.
Programs use static members when the work does not need one object's data. You do not write t.set or t.show on this page. The last page needed an object variable before the dot; this page needs the type name before :: instead. Creating an object and calling an instance function still works on other pages, but this page keeps the call on the type name so the screen stays one clear line. The next page names who can see members inside a class. Here the point is only the static header and the Tag:: call with no object built first. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine.
Above main, write class Tag with public: and static void mark() that prints pine with std::cout. Keep #include <iostream>. You do not need #include <string> on this page because there is no std::string word. There is still no second main inside Tag. The header needs the word static before void. Without static, a later Tag::mark(); call would not match this page's shape.
Inside main, write only Tag::mark(); then return 0;. Do not write Tag t("cone");. Do not write t.set("pine"); or t.show(); as the lesson path. The class name before :: is enough for the call.
Walk the run. The program starts at main. Tag::mark(); finds the static function on Tag and runs its body. The body prints pine with std::cout. No object was built, and no word member was filled. Then mark finishes, main returns, and the program ends. There is one line on the screen. It is pine. The instance path from the last page is gone from the checked file so this call shape stays clear. If you still see cone or a leftover set call, the old body was not replaced yet.
If you write mark(); with no Tag:: in front, the build fails because mark is not a free name in main. That failed experiment stays in the prose. The checked file always writes Tag::mark();.
If you drop static from the header and keep Tag::mark();, the build fails about a non-static member. The checked file always keeps static on mark.
A common mistake is to call t.mark(); after building an object as the only lesson path, which hides the type-name call. Another common mistake is to use a dot Tag.mark() like some other languages; C++ uses Tag::mark(). A third mistake is to leave void mark() without static. A fourth mistake is to put main inside Tag. A fifth mistake is to keep the old t.set / t.show body and never add mark.
This page stops before the next tool, where you choose who can see members inside the class. The next page names that tool once at the end. The static function you have just written only shows a member that runs through the type name with no object required. Keep mark small on purpose: one print, one call from main, and one line on the screen.
Open box.cpp. Keep or restore #include <iostream>. Replace the instance word / set / show body with static void mark() that prints pine. In main, write Tag::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 static header and the Tag::mark(); call.
#include <iostream>
class Tag
{
public:
static void mark()
{
std::cout << "pine" << std::endl;
}
};
int main()
{
Tag::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. Tag::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 Tag::, add the class name and ::. If the build fails about a non-static member, put static back on the header.
📊 static and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | pine, because Tag::mark runs and prints pine |
| Last | pine, because the program prints one line and stops |
| Rule | a static member function is called with the class name and needs no object |
| Screen | pine |
Try one change at a time. Change the print inside mark from "pine" to "oak", leave the call as Tag::mark();, and rebuild. The screen shows oak instead of pine, because only the print text changed. Put "pine" back. Next, temporarily write Tag t; and t.mark(); instead of Tag::mark(); if your compiler allows that form for static members, then rebuild and note that the type-name call is still the lesson path on this page. Undo that and restore Tag::mark(); so the file returns to the checked shape. Each experiment changes one thing, so the screen shows exactly whether the static print ran through the class name. Undo each change before the next one starts, so the file always returns to the checked version with one clear call.
This page ends the static tool and stops before the next one. The next page is access, which chooses who can see members inside the class. The mark function you have just written only shows a static call through Tag:: with no object required. 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 is 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