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
- Call the parent first. The first line of
Puppy::barkisDog::bark();. It prints theWoof! I am ...line using the object'sname. - Add the puppy's own output. After the parent's call, print
Yip! Saidfollowed bynameand 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 ofDog::bark();. The method calls itself without end. Name the parent class. - Forgetting the call and only printing
Yip!. That is plain overriding. TheWoof!lines are missing, and the expected output no longer matches. - Expecting the plain dog to change.
Maxis aDog, so it never runsPuppy::barkand prints only the one line. - Dropping
virtualfromDog::bark. Calls on aPuppyvariable would still work, but calls through aDogpointer 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:
| Object | Method called | Lines printed |
|---|---|---|
Puppy buddy("Buddy") | Puppy::bark | Woof! I am Buddy, then Yip! Said Buddy |
Puppy rex("Rex") | Puppy::bark | Woof! I am Rex, then Yip! Said Rex |
Dog dog("Max") | Dog::bark | Woof! 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; useParentClass::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.
