Start learning free

LESSON 38 OF 69 · OBJECT-ORIENTED PROGRAMMING

Base Keyword in C++: Calling the Parent with Dog::bark()

Practice Read the full explanation

TL ; DR:

C++ has no super keyword. To run the parent's version of an overridden method, call it by its qualified name: Dog::bark();. This lets you extend the parent's behavior instead of replacing it.


class Puppy : public Dog {
public:
    Puppy(string name) : Dog(name) {}
    void bark() override {
        Dog::bark();   // do everything the parent did
        cout << "Yip! Said " << name << endl;
    }
};

Why it matters

Overriding a method replaces the parent's behavior. Often you want to extend it instead: keep what the parent did and add something more. A typical example is a save method: the parent writes the row to the database, and a subclass runs the parent's save first and then writes a log line.

Your task

The Puppy class already overrides bark(), but the body is empty. Inside Puppy::bark():

  1. call the parent's version with Dog::bark();, so Woof! I am <name> still prints;
  2. then cout << "Yip! Said " << name << endl;.

Notes:

  • Dog::bark() means "the bark that belongs to Dog", called on the current object. Writing just bark() inside bark would call itself forever.
  • Dog's bark is virtual and Puppy's is marked override; both are already written.

The driver (already written, it runs when you press Run):

Puppy buddy("Buddy");
buddy.bark();
Puppy rex("Rex");
rex.bark();
Dog dog("Max");
dog.bark();
  • buddy is a Puppy: its bark prints the parent's line, then its own.
  • rex is another Puppy: the names come from the object, nothing is hard-coded.
  • dog is a plain Dog: its bark is unchanged and prints only the parent's line, with no Yip!.

Expected output:

Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
STEP 1 OF 2
    Ctrl + Enter to check

    C++ · OBJECT-ORIENTED PROGRAMMING

    Base Keyword in C++, explained

    Problem overview

    The starter code has a Dog class with a virtual method bark() that prints Woof! I am <name>, and a Puppy class that inherits from it. Puppy already overrides bark(), but the body is empty, so a puppy would print nothing at all. Your task is to fill that body so a puppy first does everything a dog does and then adds its own line, Yip! Said <name>.

    The code at the bottom creates two puppies, Buddy and Rex, and a plain dog, Max, and calls bark() on each. The puppies print two lines and the dog prints one, because Dog::bark itself doesn't change:

    Woof! I am Buddy
    Yip! Said Buddy
    Woof! I am Rex
    Yip! Said Rex
    Woof! I am Max

    Approach: extend, don't replace

    A method that reuses its parent's work is the everyday reason to call a parent method by name. In this lesson the parent's work is a single print, but the pattern is the same when the parent does something larger, such as validating a value or setting up shared state.

    Overriding a method replaces the parent's behaviour. Often that is what you want, but sometimes the subclass needs the parent's work plus something extra. Copying the parent's lines into the child works until the parent changes; then the two copies drift apart. A better way is to run the parent's version and add to it.

    Some languages have a keyword for that, such as super or base. C++ has neither. Instead, you name the class: Dog::bark() means "the bark that belongs to Dog", called on the current object. The :: is the scope resolution operator, and writing a class name in front of a member name tells the compiler exactly which class's version to use.

    This also explains why the lesson uses the qualified name rather than plain bark(). Inside Puppy::bark, an unqualified bark() refers to Puppy::bark itself, so the method would call itself over and over until the program ran out of stack space.

    Step by step

    1. Call the parent first. The first line of Puppy::bark is Dog::bark();. It prints the Woof! I am ... line using the object's name.
    2. Add the puppy's own output. After the parent's call, print Yip! Said followed by name and a newline.

    The order matters. Because the parent's call comes first, its line appears first. Swap the two lines and the output becomes Yip! then Woof!, which the lesson's tests don't accept.

    The solution in C++

    class Dog {
    public:
        string name;
        Dog(string name) { this->name = name; }
        virtual void bark() {
            cout << "Woof! I am " << name << endl;
        }
    };
    
    class Puppy : public Dog {
    public:
        Puppy(string name) : Dog(name) {}
        void bark() override {
            Dog::bark();
            cout << "Yip! Said " << name << endl;
        }
    };
    
    class Solution {
    public:
        void mainFunction() {
            Puppy buddy("Buddy");
            buddy.bark();
            Puppy rex("Rex");
            rex.bark();
            Dog dog("Max");
            dog.bark();
        }
    };
    

    Where name comes from

    Puppy has no name field of its own. It inherits the public field from Dog, and the constructor Puppy(string name) : Dog(name) {} hands the value to Dog's constructor through the member initialiser list. That is why Rex prints Rex and Buddy prints Buddy: nothing is hard-coded.

    Qualified calls skip the virtual lookup

    Because bark() is virtual, calling it through a Dog* that points at a Puppy runs Puppy::bark. A qualified name such as Dog::bark() turns that lookup off and calls exactly the named version. You can see both effects with a Puppy named Buddy:

    Puppy p("Buddy");
    Dog* d = &p;
    d->bark();        // virtual call: runs Puppy::bark
    p.Dog::bark();   // qualified call: runs Dog::bark only

    Together these two calls print:

    Woof! I am Buddy
    Yip! Said Buddy
    Woof! I am Buddy

    The first call prints the puppy's two lines and the second prints only the dog's line. The qualified call inside Puppy::bark relies on the same rule, which is how it reaches the parent's version even though the object is a puppy.

    The override keyword

    override asks the compiler to check that Puppy::bark really overrides a virtual function in a base class. If you misspell the name or change the parameters, you get a compile error instead of a new, unrelated method.

    Edge cases and common mistakes

    • Writing bark(); instead of Dog::bark();. The method calls itself without end. Name the parent class.
    • Forgetting the call and only printing Yip!. That is plain overriding. The Woof! lines are missing, and the expected output no longer matches.
    • Expecting the plain dog to change. Max is a Dog, so it never runs Puppy::bark and prints only the one line.
    • Dropping virtual from Dog::bark. Calls on a Puppy variable would still work, but calls through a Dog pointer or reference would run the dog's version and skip the puppy's.

    Testing

    The three objects in the lesson's code cover the cases that matter, and each result follows from the rules above:

    ObjectMethod calledLines printed
    Puppy buddy("Buddy")Puppy::barkWoof! I am Buddy, then Yip! Said Buddy
    Puppy rex("Rex")Puppy::barkWoof! I am Rex, then Yip! Said Rex
    Dog dog("Max")Dog::barkWoof! I am Max

    If your output has a different number of lines, count the calls: each puppy should run the parent's version exactly once, so a missing Dog::bark(); removes a line and a doubled one adds a line.

    Key takeaways

    • C++ has no super; use ParentClass::method() to call the parent's version.
    • Call the parent first, then add the child's behaviour, to extend a method instead of replacing it.
    • A qualified call is resolved at compile time, so it runs exactly the named class's method.
    Keep going with C++1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free