C++ virtual
The derived page left Box with inherited show printing pine and its own mark printing ok from box.cpp. This page stays in the Inheritance module on purpose. The workshop file on disk stays box.cpp so you keep one familiar build path. The new work drops mark, marks Tag::show as virtual, rewrites show on Box to print ok, and calls through a Tag* pointer. The screen shows one line: ok. The override keyword waits for the next page.
A virtual function lets a call through a parent pointer reach the version written on the real object type. On this page you write virtual void show() on Tag that still prints pine, then write void show() on Box that prints ok. Reading Tag* p = &b; is reading a parent pointer that points at a Box. Reading p->show(); is reading the virtual call that picks Box::show. Keep #include <iostream> so the print works.
Programs use virtual when you hold a parent pointer or reference but want the child’s version of a function. You do not keep the two-line show then mark body as the lesson body on this page. Extra override spelling waits for the next page on purpose. Here the point is only virtual on the parent and one call through Tag* that prints ok. The build command stays familiar: one source file box.cpp, one output name box, one run that prints ok. You compile only box.cpp again for this workshop file on disk.
Think of virtual as asking the sticky note what it really is before stamping. The previous page printed pine then ok with two different names. This page prints only ok through one virtual name. Do not expect pine on a successful run with the checked pointer call. The printed ok is part of the lesson.
Above main, write class Tag with public: and virtual void show() that prints pine. Then write class Box : public Tag with public: and void show() that prints ok. Inside main, write Box b;, then Tag* p = &b;, then p->show();, then return 0;. Keep #include <iostream> at the top. Do not write the word override in the checked body. Do not call mark on this page.
Walk the run carefully from the first line of main. The program starts at main. Box b; builds a derived object. Tag* p = &b; stores a parent pointer to that object. p->show(); uses virtual dispatch and runs Box::show, which prints ok. Then main returns and the program ends. There is one printed line on the screen: ok. The checked file always uses that pointer call.
Say the steps out loud once while you read the file. Mark Tag::show virtual. Rewrite show on Box to print ok. Point a Tag* at the Box. Call show through the pointer. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No mark. No override keyword yet. When the screen shows ok, the story matched the source.
If you forget virtual on Tag::show, the pointer call runs the parent version and the screen shows pine. That experiment can stay in the prose. The checked file keeps virtual so the screen shows ok.
If you call b.show(); only and never use Tag*, you do not practice the virtual pointer path on this page. The checked file keeps Tag* p = &b; and p->show();.
A common mistake is to expect pine from the pointer call when virtual is present. Another common mistake is to invent override early. A third mistake is to keep mark and lose the single-line virtual story. A fourth mistake is to wait for cin. A fifth mistake is to delete : public Tag so Box is no longer a Tag.
This page stops before the next tool, where the override keyword marks a rewritten virtual function on purpose. The next page names that tool once at the end. The virtual call you have just written only shows picking Box::show through a Tag*. Keep the printed ok so the order stays clear for a first careful reading of the file.
Remember that virtual changes which body runs for the same call spelling. You still write show on both types. After a successful run the screen shows ok. After you undo a bad experiment the checked file returns to that virtual pointer path. Stability of the checked file matters as much as learning virtual itself.
Open box.cpp. Remove mark and the second print from the derived demo. On Tag, change show to virtual void show() that still prints pine. On Box, replace mark with void show() that prints ok. In main, keep Box b;, add Tag* p = &b;, call p->show();, and return. The program file name on disk stays box.cpp. The checked file is below, and the new lines are virtual, the rewritten show, and the pointer call.
#include <iostream>
class Tag
{
public:
virtual void show()
{
std::cout << "pine" << std::endl;
}
};
class Box : public Tag
{
public:
void show()
{
std::cout << "ok" << std::endl;
}
};
int main()
{
Box b;
Tag* p = &b;
p->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 ok.
Check the printed ok first. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the screen shows pine, virtual is missing on Tag::show. If the build fails about Tag*, check that Box still inherits with : public Tag. If you still see two lines pine then ok, the old mark body is still there.
📊 virtual call and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | ok, because Tag* picks Box::show through virtual |
| Last | ok, because the program prints once and returns |
| Rule | virtual on Tag::show lets p->show() run Box::show |
| Screen | ok |
Try one change at a time. Temporarily remove virtual from Tag::show, rebuild, and see pine from the pointer call. Put virtual back so the screen shows ok again. Next, temporarily print ready inside Box::show, rebuild, see ready, then restore ok. Each experiment changes one thing on purpose, so you see either the parent body or a different child print. Undo each change before the next one starts, so the file always returns to the checked workshop version with virtual and ok once again after every single careful try back on disk again.
This page ends the virtual tool and stops before the next one. The next page is override, which names the rewritten virtual function on the derived class on purpose. The virtual call you have just written only shows Tag* p = &b; p->show(); printing ok from box.cpp. You leave with ok on the screen. Count the printed lines before you leave. There is exactly one printed line on that final screen, and that line is ok alone right there now after the final successful build run on disk today now after the virtual pointer check.
🧠 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