Loading...

C++ Inheritance

The filesystem page asked whether note.txt exists and printed pine from box.cpp. This page starts the Inheritance module on purpose. The workshop file on disk stays box.cpp so you keep one familiar build path. The new work names a parent type Tag with show that prints pine, then a child type Box that inherits with class Box : public Tag. In main you build a Box and call b.show(). The screen shows pine. Derived-class details wait for the next page.
Inheritance connects a child type to a parent type so the child can use members written on the parent. On this page you write class Tag with a public void show() that prints pine, then class Box : public Tag with an empty body. Reading : public Tag is reading that Box is a kind of Tag. Reading b.show() is reading the inherited call. Keep #include <iostream> so std::cout can show pine.
Programs use inheritance when one type should reuse another type’s tools without copying them. You do not keep the filesystem::exists body as the lesson body on this page. Extra derived-class pages wait for the next tool on purpose. Here the point is only the colon form and one inherited call that prints pine. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine. You compile only box.cpp again for this workshop file on disk.
Think of inheritance as a child sticky note that already knows the parent’s stamp. The previous pages left pine as a familiar word. This page prints pine through Tag::show reached from a Box object. Do not expect an empty screen on a successful run. The printed pine is part of the lesson.

Above main, write class Tag with public: and void show() that prints pine. Then write class Box : public Tag with empty braces. Inside main, write Box b;, then b.show();, then return 0;. Keep #include <iostream> at the top. Do not call filesystem::exists in the checked body. Do not rewrite show inside Box on this page.
Walk the run carefully from the first line of main. The program starts at main. Box b; builds a child object. b.show() looks for show on Box, finds nothing written there, and then finds it on the parent Tag through inheritance. The method prints pine. Then main returns and the program ends. There is one printed line on the screen: pine. The checked file always uses that empty child body and the inherited call.
Say the steps out loud once while you read the file. Declare Tag with show. Declare Box as a public Tag. Build a Box. Call show. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No exists check. No ofstream. No second show on Box yet. When the screen shows pine, the story matched the source.

If you forget : public Tag, the build fails about show on Box. The checked file keeps the colon form.
If you put show only on Box and leave Tag empty, you are not practicing inheritance on this page. The checked file keeps show on Tag and leaves Box empty.
A common mistake is to reverse the names and write class Tag : public Box. Another common mistake is to invent extends from another language. A third mistake is to keep the filesystem exists body and never declare classes. A fourth mistake is to wait for cin. A fifth mistake is to start override details that belong on later pages.
This page stops before the next tool, where derived classes get more attention. The next page names that tool once at the end. The inheritance you have just written only shows a child that keeps a parent show. Keep the word pine so the screen stays clear for a first careful reading of the file.
Remember that inheritance does not print by itself. You still call show on a Box object. After a successful run the screen shows pine. After you undo a bad experiment the checked file returns to that Tag-and-Box path. Stability of the checked file matters as much as learning the colon form itself.

Open box.cpp. Remove the filesystem exists lines from the path demo. Keep #include <iostream>. Above main, declare Tag with show that prints pine, then declare Box : public Tag with an empty body. In main, build a Box, call show, and return. The program file name on disk stays box.cpp. The checked file is below, and the new lines are the parent, the child, and the inherited call.

box.cpp Box inherits Tag show pine

cpp
#include <iostream>

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

class Box : public Tag
{
};

int main()
{
    Box b;
    b.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 pine.
Check the printed 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 show, check : public Tag. If the screen is empty, the call b.show(); is missing. If you still see a filesystem exists path, the old body is still there.

📊 inheritance and what the screen shows

Piece What the screen shows
First pine, because Box uses Tag::show through inheritance
Last pine, because the program prints once and returns
Rule class Box : public Tag lets Box call show written on Tag
Screen pine

Try one change at a time. Temporarily print oak inside Tag::show, rebuild, and see oak. Restore pine. Next, temporarily remove : public Tag so the build fails about show, then put the colon form back. Each experiment changes one thing on purpose, so you see either a different print or a missing-inheritance build error. Undo each change before the next one starts, so the file always returns to the checked workshop version with empty Box and pine once again after every single careful try back on disk again.
This page ends the inheritance tool and stops before the next one. The next page is derived, which looks more closely at derived classes. The inheritance you have just written only shows Box keeping Tag::show from box.cpp and printing pine. You leave with pine on the screen. Count the printed lines before you leave. There is exactly one printed line on that final screen, and that line is pine alone right there now after the final successful build run on disk today now after the public inheritance check.

🧠 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