C++ Abstract Classes
The override page left virtual void show() on Tag with a pine body, 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 turns Tag::show into a pure virtual with = 0, so Tag alone cannot be built. Box still overrides and prints ok through the pointer. Polymorphism waits for the next page.
An abstract class is a type that must not be constructed alone because at least one member is pure virtual. On this page you write virtual void show() = 0; on Tag with no body, then keep void show() override on Box that prints ok. Reading = 0 is reading the pure virtual mark. Reading Tag* p = &b; is still reading a parent pointer to a concrete Box. Keep #include <iostream> so the print works.
Programs use abstract classes when the parent names a required tool but refuses to supply a default body. You do not keep a pine body on Tag::show as the lesson body on this page. Extra polymorphism naming waits for the next page on purpose. Here the point is only the pure virtual mark and one concrete override 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 an abstract class as a sticky note label that cannot stand alone until a child fills the stamp. The previous page still let Tag print pine if called directly. This page removes that body. Do not expect to write Tag t; in the checked file. The printed ok through Box is part of the lesson.
Above main, write class Tag with public: and virtual void show() = 0;. 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 construct a Tag object. Do not invent a second polymorphism demo on this page.
Walk the run carefully from the first line of main. The program starts at main. Box b; builds the only concrete object. Tag* p = &b; stores a parent pointer. p->show(); picks Box::show and prints ok. Then main returns and the program ends. There is one printed line on the screen: ok. The checked file never builds a bare Tag.
Say the steps out loud once while you read the file. Mark Tag::show pure virtual with = 0. Override 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 Tag object. No pine body on Tag. When the screen shows ok, the story matched the source.
If you write Tag t; in main, the build fails because Tag is abstract. That failed experiment stays in the prose. The checked file only builds Box.
If you forget = 0 and leave a pine body on Tag, the type is no longer abstract on this page. The checked file keeps the pure virtual form.
A common mistake is to call show without a concrete derived object. Another common mistake is to invent the word polymorphism as a required keyword here. A third mistake is to drop override from Box::show. A fourth mistake is to wait for cin. A fifth mistake is to delete : public Tag.
This page stops before the next tool, where one name can mean different concrete behaviors through the same call spelling. The next page names that tool once at the end. The abstract class you have just written only shows a pure virtual parent and one concrete child. Keep the printed ok so the order stays clear for a first careful reading of the file.
Remember that pure virtual removes the parent body. You still need a derived override to run. After a successful run the screen shows ok. After you undo a bad experiment the checked file returns to that abstract path. Stability of the checked file matters as much as learning = 0 itself.
Open box.cpp. On Tag, replace the pine body with virtual void show() = 0;. Keep void show() override on Box printing ok. Keep Tag* p = &b; and p->show(); in main. Do not add Tag t;. The program file name on disk stays box.cpp. The checked file is below, and the new mark is = 0 on the parent show.
#include <iostream>
class Tag
{
public:
virtual void show() = 0;
};
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 an abstract type, you tried to construct Tag or forgot the override on Box. If the screen is empty, the pointer call is missing. If you still see a pine body on Tag, the pure virtual mark is not in place.
📊 abstract Tag and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | ok, because Box overrides the pure virtual show |
| Last | ok, because the program prints once and returns |
| Rule | virtual void show() = 0 makes Tag abstract; Box supplies show |
| Screen | ok |
Try one change at a time. Temporarily add Tag t; in main, rebuild, and see an abstract-type build failure. Remove that line. Next, temporarily print ready inside Box::show, rebuild, see ready, then restore ok. Each experiment changes one thing on purpose, so you see either an abstract 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 = 0 and ok once again after every single careful try back on disk again.
This page ends the abstract tool and stops before the next one. The next page is polymorphism, which names the idea of one call spelling reaching different concrete bodies. The abstract class you have just written only shows virtual void show() = 0 on Tag from box.cpp while Box still prints ok through Tag*. 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 pure virtual 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