CJCoding With Joseph
15free / daySign inCreate account
โ† Back to Question List
C++ Code Walkthrough #10

Inheritance and Overriding

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