CJCoding With Joseph
15free / daySign inCreate account
Back to Code Walkthrough

Inheritance and Overriding

C++ code walkthrough · Question 10
hardClasses

Code

Read carefully. What does this print?
1#include <iostream>
2using namespace std;
3 
4class Animal {
5public:
6 void speak() {
7 cout << "..." << endl;
8 }
9};
10 
11class Dog : public Animal {
12public:
13 void speak() {
14 cout << "Woof!" << endl;
15 }
16};
17 
18int main() {
19 Animal a;
20 Dog d;
21 a.speak();
22 d.speak();
23 Animal* ptr = &d;
24 ptr->speak();
25 return 0;
26}

Your answer