C++ Members
The objects page left one line on the screen in box.cpp: ok, from t.show() with a fixed print. This page stays in the same file and the same Classes module. The type name Tag and the object t stay. The new work adds a data box inside the class. You fill that box on the object, then let show print what the object holds. The screen shows one line: pine.
A member is a name that belongs to each object of the class. On earlier pages a member function was a tool. This page adds a data member: std::string word under public:. After Tag t;, you write t.word = "pine"; so that object’s box holds pine. Then t.show(); prints that box. The Classes cast keeps Tag and box.cpp. The word pine returns as the value stored on the object, not as a leftover free-function print from types.cpp. Reading t.word is reading data that lives with that object for the whole run after you set it.
Programs put data on objects so each value can remember its own state. Two Tag objects can hold two different words. This page builds one object and one word so the screen stays one clear line. Filling the word through a constructor waits for the next page. Here the point is the member name after the object, with a dot, and the print that reads that member. The build command stays familiar: one source file box.cpp, one output name box, one run that now prints pine.
Above main, keep class Tag. Inside, under public:, add std::string word; before show. Change show so it prints word instead of a fixed ok: std::cout << word << std::endl;. Keep #include <string> at the top next to iostream, because std::string needs it. Keep void on show. There is still no second main inside Tag.
Inside main, keep Tag t;, then set the member: t.word = "pine";, then call t.show();, then return 0;. The assignment writes into the object’s data box. The call prints that box. You are not printing "ok" from a fixed string inside show on this page.
Walk the run. The program starts at main. Tag t; builds one object. t.word = "pine"; stores pine in that object. t.show(); enters show, reads word, and prints pine. Then show finishes, main returns, and the program ends. There is one line on the screen. It is pine, not ok. The fixed print is gone from the checked file. Inside show, the name word means this object’s member, because the call came through t.
If you call t.show(); before setting t.word, the screen may show an empty line, because the string starts empty. That experiment stays in the prose. The checked file always sets t.word before the call.
If you write word = "pine"; inside main without t., the build fails, because word is not a free name in main. The checked file always uses t.word.
A common mistake is to leave the fixed "ok" print in show and still set t.word, so the screen never shows pine. Another common mistake is to treat word as a global and forget the object prefix. A third mistake is to set t.word after the call, so the print still shows the empty string. A fourth mistake is to jump ahead into a constructor that fills word for you; this page sets the member with an assignment. A fifth mistake is to forget #include <string> after adding std::string word, so the build fails before you ever see pine.
This page stops before the next tool, where a constructor fills members when the object is built. The next page names that tool once at the end. The member you have just written only shows data that lives on the object. Keep box.cpp as the Classes file for the pages that follow.
Open box.cpp. Keep the includes, and add #include <string> if it is missing. Inside Tag, add std::string word; and change show to print word. In main, after Tag t;, set t.word = "pine"; before t.show();. The file name on disk stays box.cpp. The checked file is below, and the new lines are the word member, the print of word, and the assignment in main.
#include <iostream>
#include <string>
class Tag
{
public:
std::string word;
void show()
{
std::cout << word << std::endl;
}
};
int main()
{
Tag t;
t.word = "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. t.show printed the word stored in t.word. The table gives each row its piece, so compare your run with it line by line. If the screen shows ok, the old fixed print is still in show. If the screen is blank, t.word was never set before the call. If the build fails about string, the #include <string> line is missing. If the build fails about word in main, you forgot the t. prefix on the assignment.
📊 The member and what it shows
| Piece | What the screen shows |
|---|---|
| First | pine, because t.word holds pine and show prints that member |
| Last | pine, because the program prints one line and stops |
| Rule | a data member lives on each object and is reached with object.member |
| Screen | pine |
Try one change at a time. Change t.word = "pine"; to t.word = "cone";, leave show as it is, and rebuild. The screen shows cone instead of pine, because only the stored value changed. Put "pine" back. Next, add a second object: after the first call, write Tag u;, set u.word = "cone";, call u.show();, leave the first object as it is, and rebuild. The screen shows pine, then cone, because each object keeps its own word. Remove the second object before you leave so the file returns to the checked one-line shape. Each experiment changes one thing, so the screen shows exactly which object’s member you printed. Undo each change before the next one starts, so the file always returns to the checked version.
This page ends the members tool and stops before the next one. The next page is constructors, which fills members when the object is built. The word member you have just written only shows data stored on the object after creation. You leave with one line on the screen: pine. Count the lines before you leave. There is one, and it 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