C++ Polymorphism
The abstract page left virtual void show() = 0; on Tag, void show() override on Box 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 names polymorphism: one call spelling p->show() can reach different concrete bodies. Keep the pure virtual on Tag. Keep Box printing ok. Add a second derived class Cap that overrides show and prints pine. Point the same Tag* at Box, call show, then point it at Cap and call show again. Enums wait for the next page.
Polymorphism here means the same parent pointer call can select different child bodies. Reading Tag* p = &b; still reads a parent pointer to a concrete Box. Reading p = &c; then reads that same pointer type aimed at a concrete Cap. Reading p->show(); twice is reading one spelling that prints ok first and pine second. Keep #include <iostream> so both prints work. Do not invent an enums keyword on this page.
Programs use this pattern when one parent type names a required tool and several children supply different answers. You still do not construct a bare Tag, because the pure virtual mark stays. You do not need a new workshop file name. The build command stays familiar: one source file box.cpp, one output name box, one run that prints two lines. You compile only box.cpp again for this workshop file on disk.
Think of polymorphism as one sticky-note call that can stamp different concrete answers. The previous page showed one child. This page shows two children under the same call spelling. Do not expect enums in the checked file. The printed ok then pine through Tag* is the whole lesson point.
Above main, keep class Tag with public: and virtual void show() = 0;. Keep class Box : public Tag with void show() override that prints ok. Add class Cap : public Tag with void show() override that prints pine. Inside main, write Box b;, Cap c;, then Tag* p = &b;, then p->show();, then p = &c;, then p->show();, then return 0;. Keep #include <iostream> at the top. Do not construct a Tag object. Do not invent an enums demo on this page.
Walk the run carefully from the first line of main. The program starts at main. Box b; and Cap c; build the two concrete objects. Tag* p = &b; stores a parent pointer to the box. The first p->show(); picks Box::show and prints ok. Then p = &c; aims the same pointer at the cap. The second p->show(); picks Cap::show and prints pine. Then main returns and the program ends. There are two printed lines on the screen: ok then pine. The checked file never builds a bare Tag.
Say the steps out loud once while you read the file. Keep Tag::show pure virtual with = 0. Keep Box printing ok. Add Cap printing pine. Point Tag at Box, call show. Point Tag at Cap, call show. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No Tag object. No enums keyword. When the screen shows ok then pine, the story matched the source.
If you write Tag t; in main, the build fails because Tag is still abstract. That failed experiment stays in the prose. The checked file only builds Box and Cap.
If you forget the second call after p = &c;, the screen shows only ok. The checked file keeps both calls so polymorphism is visible.
A common mistake is to call show only through Box and Cap variables and never through Tag*. Another common mistake is to invent the word enums as a required keyword here. A third mistake is to drop override from one child. A fourth mistake is to wait for cin. A fifth mistake is to delete : public Tag from Cap.
This page stops before the next tool, where named constant values get a small typed list. The next page names that tool once at the end. The polymorphism you have just written only shows one pure virtual parent and two concrete children under the same call spelling. Keep the printed ok then pine so the order stays clear for a first careful reading of the file.
Remember that polymorphism reuses the parent call spelling. You still need concrete overrides to run. After a successful run the screen shows ok then pine. After you undo a bad experiment the checked file returns to that two-child path. Stability of the checked file matters as much as learning the name polymorphism itself.
Open box.cpp. Keep virtual void show() = 0; on Tag. Keep void show() override on Box printing ok. Add class Cap : public Tag with void show() override printing pine. In main, keep Box b;, add Cap c;, keep Tag* p = &b; p->show();, then add p = &c; p->show();. Do not add Tag t;. The program file name on disk stays box.cpp. The checked file is below, and the new name for this idea is polymorphism.
#include <iostream>
class Tag
{
public:
virtual void show() = 0;
};
class Box : public Tag
{
public:
void show() override
{
std::cout << "ok" << std::endl;
}
};
class Cap : public Tag
{
public:
void show() override
{
std::cout << "pine" << std::endl;
}
};
int main()
{
Box b;
Cap c;
Tag* p = &b;
p->show();
p = &c;
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 then pine.
Check the printed ok then pine 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 an abstract type, you tried to construct Tag or forgot an override on a child. If the screen shows only ok, the second pointer call is missing. If you never see pine, Cap is missing or the pointer never aimed at it.
📊 polymorphism Tag and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | ok, because Tag* first aims at Box::show |
| Last | pine, because Tag* then aims at Cap::show |
| Rule | same p->show() spelling reaches different concrete bodies |
| Screen | ok then pine |
Try one change at a time. Temporarily remove the second p->show();, rebuild, and see only ok. Put the call back. Next, temporarily print ready inside Cap::show, rebuild, see ok then ready, then restore pine. Each experiment changes one thing on purpose, so you see either a shorter print or a different second line. Undo each change before the next one starts, so the file always returns to the checked workshop version with two children and ok then pine once again after every single careful try back on disk again.
This page ends the polymorphism tool and stops before the next one. The next page is enums, which names a small typed list of constant values. The polymorphism you have just written only shows virtual void show() = 0 on Tag from box.cpp while Box prints ok and Cap prints pine through the same Tag*. You leave with ok then pine on the screen. Count the printed lines before you leave. There are exactly two printed lines on that final screen, and those lines are ok then pine alone right there now after the final successful build run on disk today now after the two-child 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