Loading...

C++ Objects

The classes page left one line on the screen in box.cpp: ready, from Tag::show(). This page stays in the same file and the same Classes module. The type name Tag stays. The new work builds one value of that type, stores it in a variable, and calls a member function through that variable. The static call with Tag:: is replaced. The screen shows one line: ok.
An object is one value built from a class. The class is still the named type in the file. The object is one concrete piece for this run. On this page you write Tag t; to create that piece, then t.show(); to run a tool that belongs to each object. The word pine from the older free-function workshop is not required here; the Classes cast keeps Tag and box.cpp so the module stays one example. Reading t in a call is reading the object you built, not the type name alone.
Programs need objects when each value can act on its own. Two different Tag variables would be two different objects. This page builds one object first so the screen stays one clear line. Creating data fields inside the class waits for the next page. Here the point is the object variable and the call that uses a dot after that variable. The build command stays the same as the classes page: one source file box.cpp, one output name box, one run that now prints ok.

Above main, keep class Tag. Inside, under public:, write void show() without the word static. The body prints one word: std::cout << "ok" << std::endl;. Keep void. The function does not return a number. It only writes. There is still no second main inside Tag.
Inside main, create the object with Tag t;, then call t.show();, then return 0;. The first line declares a variable t of type Tag and builds that object. The second line calls show on that object. The dot sits after the variable name. You are not writing Tag::show(); on this page. That was the static shape from the classes lesson.
Walk the run. The program starts at main. Tag t; builds one object. t.show(); enters show for that object and prints ok. Then show finishes, main returns, and the program ends. There is one line on the screen. It is ok, not ready. The old static call is gone from the checked file. The braces of Tag still mark where the type’s tools live, but the call now names the object first, then the tool, with a dot between them.

If you write Tag::show(); while show is not static, the build fails. That failed experiment stays in the prose, not in the checked file. The checked file always builds Tag t; and calls t.show();.
If you write only Tag t; and never call show, the program runs but the screen stays empty of that word. On this page keep both lines so the object’s tool is visible.
A common mistake is to keep static on show and still write t.show();, which can work in some shapes but hides the lesson: this page removes static on purpose. Another common mistake is to leave Tag::show(); from the previous page so the screen still shows ready. A third mistake is to think the class name alone is the object; Tag is the type, and t is the object variable. A fourth mistake is to jump ahead into member fields; this page only builds the object and calls show. A fifth mistake is to forget public: again so show stays private and t.show(); fails to compile.
This page stops before the next tool, where the class holds data you set and read on each object. The next page names that tool once at the end. The object you have just built only shows a value you can call through. Keep box.cpp as the Classes file for the pages that follow.

Open box.cpp. Keep #include <iostream> at the top. Change show so it is not static, and change the printed word to ok. In main, replace Tag::show(); with Tag t; and t.show();. The file name on disk stays box.cpp. The checked file is below, and the new lines are the instance show and those two lines in main.

box.cpp builds Tag t and calls t.show

cpp
#include <iostream>

class Tag
{
public:
    void show()
    {
        std::cout << "ok" << std::endl;
    }
};

int main()
{
    Tag t;
    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: ok.
Check that line first. t.show printed ok. The table gives each row its piece, so compare your run with it. If the screen shows ready, the old static call is still running. If the build fails about show with Tag::, you called the type name while show is an instance tool. If t is missing, there is no object to call through.

📊 The object and what it shows

Piece What the screen shows
First ok, because t.show runs as the first and only print
Last ok, because the program prints one line and stops
Rule an object is one value of a class, and you call its tools through the object variable
Screen ok

Try one change at a time. Change the word inside show from "ok" to "pine", leave Tag t; and t.show(); as they are, and rebuild. The screen shows pine instead of ok, because only the print text changed. Put "ok" back. Next, add a second object: after the first call, write Tag u; and u.show();, leave the first object as it is, and rebuild. The screen shows two lines of ok, because each object can run show. 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 how objects and calls relate. Undo each change before the next one starts, so the file always returns to the checked version.
This page ends the objects tool and stops before the next one. The next page is members, which puts data inside the class for each object to hold. The Tag t you have just written only shows one built value and a call through that value. You leave with one line on the screen: ok. Count the lines before you leave. There is one, and it is ok.

🧠 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