Loading...

C++ Member Functions

The this page left one line on the screen in box.cpp: pine, after Tag t("pine"); and t.show(), with this->word = word; inside the constructor. This page stays in the same file and the same Classes module. The type name Tag, the member word, the constructor, and show stay. The new work adds another member function that can change word after the object already exists. The screen still shows one line: pine.
A member function is a function that belongs to the class and runs on one object. You already used show to print. On this page you write void set(std::string word) and inside it this->word = word;. Then main builds with Tag t("cone");, calls t.set("pine");, and finally t.show();. The Classes cast keeps Tag and box.cpp. The value pine ends on the object because set wrote it there after creation. Reading t.set("pine") is reading a call that uses the object through a dot, just like t.show().
Programs use member functions so an object can do work later, not only at construction time. You do not put every change inside the constructor on this page. The constructor still gives the first value, and set gives a later value when you need one. Creating two objects and calling set on each can store two different words, but this page builds one object so the screen stays one clear line. The next page names a tool that belongs to the type without needing one object. Here the point is only a member function you call after the object exists, through a dot on the variable. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine.

Above main, keep class Tag with std::string word;, the constructor that uses this->word = word;, and show that prints word. Add void set(std::string word) under public: with braces, and inside write this->word = word;. Keep the includes for iostream and string. There is still no second main inside Tag. The new function has a return type line void. Its name is set, not Tag.
Inside main, write Tag t("cone");, then t.set("pine");, then t.show();, then return 0;. The constructor stores cone first. The call to set replaces it with pine. The call to show prints pine. You are not finishing with cone on the screen on this page.
Walk the run. The program starts at main. Tag t("cone"); builds one object and runs the constructor, which sets word to cone. t.set("pine"); enters set on that object, and this->word = word; stores pine in place of cone. t.show(); prints pine. Then show finishes, main returns, and the program ends. There is one line on the screen. It is pine, not cone. The first constructor value is gone from the member by then. By the time show runs, set has already changed the member on that same object.

If you call set("pine"); with no t. in front, the build fails because set is not a free name in main. That failed experiment stays in the prose. The checked file always writes t.set("pine");.
If you skip t.set("pine"); and only build with Tag t("cone"); then t.show();, the screen shows cone. The checked file always calls set before show.
A common mistake is to name the new function Tag again, which fights the constructor. Another common mistake is to call Tag::set("pine"); as if it were static. A third mistake is to forget void on set. A fourth mistake is to change word only in main with t.word = "pine"; and skip writing set. A fifth mistake is to put main inside Tag again.
This page stops before the next tool, where a member can belong to the type without one object in front. The next page names that tool once at the end. The set function you have just written only shows a member function that changes the object after creation. Keep show as the print helper, and keep set as the change helper, so each name has one clear job.

Open box.cpp. Keep the includes, word, the constructor with this->word, and show. Add void set(std::string word) that writes this->word = word;. In main, write Tag t("cone");, then t.set("pine");, then t.show();. The file name on disk stays box.cpp. The checked file is below, and the new lines are the set function and the call that passes "pine".

box.cpp adds set so Tag can change word after creation

cpp
#include <iostream>
#include <string>

class Tag
{
public:
    std::string word;
    Tag(std::string word)
    {
        this->word = word;
    }
    void set(std::string word)
    {
        this->word = word;
    }
    void show()
    {
        std::cout << word << std::endl;
    }
};

int main()
{
    Tag t("cone");
    t.set("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. set stored pine, and t.show printed it. The table gives each row its piece, so compare your run with it line by line. If the screen shows cone, set was never called. If the build fails about set with no object, you wrote a bare set("pine");. If the build fails about Tag::set, remove the type-only call and use t.set("pine");.

📊 set and what the screen shows

Piece What the screen shows
First pine, because t.set("pine") updates word before show
Last pine, because the program prints one line and stops
Rule a member function runs on one object and can change its members
Screen pine

Try one change at a time. Change t.set("pine"); to t.set("oak");, leave the constructor argument as cone, and rebuild. The screen shows oak instead of pine, because only the set argument changed. Put "pine" back. Next, after t.show();, add Tag u("cone");, u.set("oak");, and u.show();, leave the first object as it is, and rebuild. The screen shows pine, then oak, because each object runs its own member function. 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 set call wrote the final word. Undo each change before the next one starts, so the file always returns to the checked version.
This page ends the member-functions tool and stops before the next one. The next page is static, which names a member that belongs to the type without needing one object in front. The set function you have just written only shows changing word after creation through a member call on the object variable. You leave with one line on the screen: pine. Count the lines before you leave. There is exactly one printed line, and it is pine.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 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