Loading...

C++ Derived Classes

The inheritance page left an empty Box : public Tag that only called show and printed pine 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 Tag::show printing pine, then fills the derived Box with its own mark that prints ok. In main you call b.show(); then b.mark();. The screen shows pine, then ok. Virtual waits for the next page.
A derived class is still a child through : public Tag, but it can also hold members of its own. On this page you keep class Tag with show, then write class Box : public Tag with a public void mark() that prints ok. Reading mark on Box is reading a tool that lives only on the derived type. Reading b.show() is still reading the inherited call. Keep #include <iostream> so both prints work.
Programs use derived classes when the child needs the parent tools and also needs a little extra of its own. You do not leave Box empty as the lesson body on this page. Extra virtual tools wait for the next page on purpose. Here the point is only adding mark on Box while keeping inherited show. 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 the derived class as a child sticky note that still uses the parent stamp and also adds its own mark. The previous page left pine alone. This page prints pine and then ok. Do not expect a single-line screen on a successful run. Both printed lines are part of the lesson.

Above main, keep class Tag with public: and void show() that prints pine. Then write class Box : public Tag with public: and void mark() that prints ok. Inside main, write Box b;, then b.show();, then b.mark();, then return 0;. Keep #include <iostream> at the top. Do not write virtual in the checked body. Do not remove : public Tag.
Walk the run carefully from the first line of main. The program starts at main. Box b; builds a derived object. b.show() finds show on the parent Tag and prints pine. b.mark() finds mark on Box and prints ok. Then main returns and the program ends. There are two printed lines on the screen: pine, then ok. The checked file always keeps both calls in that order.
Say the steps out loud once while you read the file. Keep Tag with show. Fill Box with mark. Build a Box. Call show. Call mark. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No empty child. No virtual. No override keyword yet. When the screen shows pine then ok, the story matched the source.

If you forget mark on Box but still call b.mark();, the build fails about mark. The checked file keeps that member on the derived class.
If you remove : public Tag, b.show(); fails even though mark still works. The checked file keeps both inheritance and the derived member.
A common mistake is to put mark on Tag and leave Box empty again. Another common mistake is to invent virtual early. A third mistake is to reverse the call order and expect ok first. A fourth mistake is to wait for cin. A fifth mistake is to copy show into Box instead of inheriting it.
This page stops before the next tool, where virtual changes how calls choose a function. The next page names that tool once at the end. The derived class you have just written only shows keeping parent show and adding mark. Keep pine then ok so the order stays clear for a first careful reading of the file.
Remember that a derived member does not replace inheritance by itself. You still keep : public Tag and you still call show. After a successful run the screen shows pine then ok. 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 derived member itself.

Open box.cpp. Keep Tag with show that prints pine. Fill the empty Box : public Tag body with public: and void mark() that prints ok. In main, keep Box b;, call show, then call mark, and return. The program file name on disk stays box.cpp. The checked file is below, and the new lines are the derived mark and the second call.

box.cpp derived Box mark after Tag show

cpp
#include <iostream>

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

class Box : public Tag
{
public:
    void mark()
    {
        std::cout << "ok" << std::endl;
    }
};

int main()
{
    Box b;
    b.show();
    b.mark();
    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, then ok.
Check the two printed lines 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 mark, check that mark is inside Box. If only pine appears, the b.mark(); call is missing. If only ok appears, the b.show(); call is missing or inheritance was removed.

📊 derived Box and what the screen shows

Piece What the screen shows
First pine, because inherited Tag::show runs first
Last ok, because derived Box::mark runs second
Rule a derived class can keep parent show and add its own mark
Screen pine then ok

Try one change at a time. Temporarily print oak inside Tag::show, rebuild, and see oak then ok. Restore pine. Next, temporarily print ready inside mark, rebuild, see pine then ready, then restore ok. Each experiment changes one thing on purpose, so you see either a different first line or a different second line. Undo each change before the next one starts, so the file always returns to the checked workshop version with pine then ok once again after every single careful try back on disk again.
This page ends the derived tool and stops before the next one. The next page is virtual, which changes how a call can pick a function through a base pointer or reference. The derived class you have just written only shows Box keeping Tag::show and adding mark from box.cpp. You leave with pine then ok on the screen. Count the printed lines before you leave. There are exactly two printed lines on that final screen, pine then ok alone right there now after the final successful build run on disk today now after the derived mark 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