C++ override
The virtual page left Tag::show marked virtual, Box::show printing ok, and Tag* p = &b; p->show(); 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 keeps that same virtual call and adds the word override after Box::show. The screen still shows one line: ok. Abstract classes wait for the next page.
The override keyword tells the compiler that this derived function is meant to rewrite a virtual parent function. On this page you keep virtual void show() on Tag that prints pine, then write void show() override on Box that prints ok. Reading override is reading that spelling check. Reading p->show(); is still reading the virtual call that prints ok. Keep #include <iostream> so the print works.
Programs use override so a misspelled derived name fails at build time instead of quietly adding a new function. You do not drop virtual from Tag on this page. Extra abstract-class tools wait for the next page on purpose. Here the point is only naming the rewrite with override while the pointer call still 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 override as labeling the child stamp as the intentional rewrite of the parent stamp. The previous page already printed ok through the pointer. This page keeps that print and adds the label. Do not expect a new screen word on a successful run. The printed ok is still part of the lesson.
Above main, keep class Tag with public: and virtual void show() that prints pine. Then write class Box : public Tag with public: and void show() override 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 invent an abstract class on this page. Do not remove virtual from Tag.
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. p->show(); still picks Box::show through virtual dispatch and prints ok. Then main returns and the program ends. There is one printed line on the screen: ok. The checked file always keeps override on that derived show.
Say the steps out loud once while you read the file. Keep Tag::show virtual. Mark Box::show with override. 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 abstract keyword yet. No pure virtual yet. When the screen shows ok, the story matched the source.
If you misspell the derived name so it no longer matches the virtual parent, override makes the build fail. That failed experiment stays in the prose. The checked file keeps a correct show() override.
If you remove override but keep the same correct show, the program can still print ok. The checked file keeps override so the rewrite is named on purpose.
A common mistake is to put override on the parent instead of the derived function. Another common mistake is to invent abstract early. A third mistake is to drop virtual from Tag and still expect a virtual pointer call. A fourth mistake is to wait for cin. A fifth mistake is to delete : public Tag.
This page stops before the next tool, where a class can refuse to be built alone as an abstract type. The next page names that tool once at the end. The override you have just written only shows labeling Box::show as the rewrite. Keep the printed ok so the order stays clear for a first careful reading of the file.
Remember that override does not change the printed word by itself. You still need virtual on the parent and a matching derived body. After a successful run the screen shows ok. After you undo a bad experiment the checked file returns to that override path. Stability of the checked file matters as much as learning override itself.
Open box.cpp. Keep virtual void show() on Tag. On Box, change void show() to void show() override that still prints ok. Keep Tag* p = &b; and p->show(); in main. The program file name on disk stays box.cpp. The checked file is below, and the new word is override after the derived show.
#include <iostream>
class Tag
{
public:
virtual void show()
{
std::cout << "pine" << std::endl;
}
};
class Box : public Tag
{
public:
void show() override
{
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 build fails about override, check that Tag::show is still virtual and the derived name is still show. If the screen shows pine, virtual is missing. If override is missing but the screen still shows ok, put the keyword back for this page’s checked shape.
📊 override and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | ok, because Tag* still picks Box::show |
| Last | ok, because the program prints once and returns |
| Rule | override marks Box::show as rewriting virtual Tag::show |
| Screen | ok |
Try one change at a time. Temporarily rename the derived function to showw while keeping override, rebuild, and see a build failure. Restore show() override. Next, temporarily print ready inside the overridden show, rebuild, see ready, then restore ok. Each experiment changes one thing on purpose, so you see either an override build error or a different print. Undo each change before the next one starts, so the file always returns to the checked workshop version with override and ok once again after every single careful try back on disk again.
This page ends the override tool and stops before the next one. The next page is abstract, which starts a class that cannot be built alone. The override you have just written only shows labeling Box::show from box.cpp while Tag* p = &b; p->show(); still prints ok. 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 override spelling 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