← Back to Question List
C++ Code Walkthrough #10
Inheritance and Overriding
📄 Code
Read carefully — what does this print?1#include <iostream>2using namespace std;34class Animal {5public:6 void speak() {7 cout << "..." << endl;8 }9};1011class Dog : public Animal {12public:13 void speak() {14 cout << "Woof!" << endl;15 }16};1718int main() {19 Animal a;20 Dog d;21 a.speak();22 d.speak();23 Animal* ptr = &d;24 ptr->speak();25 return 0;26}