C++ Classes
The recursion page left fifty-one lines on the screen in types.cpp, and the last one was 1 from down(3). This page does not grow that file. The Classes module starts a new example file named box.cpp, so the free-function workshop with pine and the countdown stays behind. You still know how main starts a program. The new work names a second type with class, puts a small printing tool on that type, and asks for it from main by the type name. The screen shows one line: ready.
A class is a named type you declare with class. Inside its braces you can hold member functions that belong to that type. On this page the member function is static, so you call it with the type name and :: without building an object yet. Building a value of the type waits for the next page. Here the point is only the named type beside main. Reading Tag in a call is reading a type name, not a variable name that holds pine or cone from the earlier file.
Programs split work across types so one type can start and another can own a helper. main still starts the program. Tag owns show. The call Tag::show(); jumps into that helper and prints ready. The word pine from the old workshop is not printed on this page; the new screen is a single ready line so the new idea stays clear. Keeping the file short also makes the build command easy to type: one source file, one output name box, one run.
Above main, write class Tag with braces. Inside, under public:, write static void show(). The body prints one word: std::cout << "ready" << std::endl;. Keep void on this page. The function does not return a number. It only writes. There is no second main inside Tag. A class can hold tools without being the starting point.
Inside main, keep the includes at the top of the file, then call Tag::show(); and return 0;. The call names the type Tag, then ::, then show. You are not writing show(); alone unless show also lives as a free function. On this page show belongs to Tag, so the call carries the type name.
Walk the run. The program starts at main. The only work before return is Tag::show();. That call enters Tag’s show and prints ready. Then show finishes, main returns, and the program ends. There is one line on the screen. There is no countdown. There is no pine print from the old file. The braces of Tag mark where the type’s tools live, just as function braces marked scope on the earlier pages, but here the name you call with is the class name itself.
If you call show(); without Tag::, the build fails unless a free show exists. That failed experiment stays in the prose, not in the checked file. The checked file always uses Tag::show();.
If you put main only inside Tag and leave the outer main empty, the usual beginner build that links main from the free function will not match this page’s shape. Keep main outside the class, and keep show inside Tag.
A common mistake is to write new Tag or Tag t; on this page and think that is required to call show. Static member functions do not need an object. Creating an object is the next tool. Another common mistake is to leave down(3); from types.cpp in this new file; delete the old countdown if you pasted it by habit. A third mistake is to print the letters Tag with quotes instead of calling show, which would show Tag rather than ready. A fourth mistake is to forget public: so show stays private and the call from main fails to compile.
This page stops before the next tool, where you create a value of a class type and use its data. The next page names that tool once at the end. The class you have just written only shows a named type with a static helper. Keep box.cpp on disk as the Classes starting point for the pages that follow.
Type a new file named box.cpp. Put #include <iostream> at the top. Write class Tag with public: and static void show() that prints ready. Then write int main() that calls Tag::show(); and returns 0. Do not keep the fifty-one-line types.cpp body in this file. The checked file is below, and every line belongs to this new Classes example.
#include <iostream>
class Tag
{
public:
static void show()
{
std::cout << "ready" << std::endl;
}
};
int main()
{
Tag::show();
return 0;
}
Build 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: ready.
Check that line first. Tag::show printed ready. The table gives each row its piece, so compare your run with it. If the screen shows 3, then 2, then 1, a leftover down call is still running. If the build fails about show, the call is missing Tag:: or show is not inside Tag. If the file is still named types.cpp with the old body, start from box.cpp as written below.
📊 The class beside main and what it shows
| Piece | What the screen shows |
|---|---|
| First | ready, because Tag::show runs as the first and only print |
| Last | ready, because the program prints one line and stops |
| Rule | a class is a named type that can hold a static member function |
| Screen | ready |
Try one change at a time. Change the word inside show from "ready" to "ok", leave the call as Tag::show();, and rebuild. The screen shows ok instead of ready, because only the print text changed. Put "ready" back. Next, change the call to print "pine" from main with std::cout and comment out Tag::show();, then rebuild. The screen shows pine, and you see that the class helper was not used. Undo that and restore Tag::show();. Each experiment changes one thing, so the screen shows exactly whether the type helper ran. Undo each change before the next one starts, so the file always returns to the checked version.
This page ends the first Classes tool and stops before the next one. The next page is objects, which creates a value of a class type and uses what it holds. The Tag you have just written only shows a named type with a static call. You leave with one line on the screen: ready. Count the lines before you leave. There is one, and it is ready.
🧠 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