CJCoding With Joseph
15per day
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
C++ Quiz #27

Non-Virtual Method Call

easyPolymorphism

The programmer expects this code to print "Dog speaks", but it prints "Animal speaks" instead. What is the bug?

📄 Code

1#include <iostream>
2using namespace std;
3 
4class Animal {
5public:
6 void speak() {
7 cout << "Animal speaks" << endl;
8 }
9};
10 
11class Dog : public Animal {
12public:
13 void speak() {
14 cout << "Dog speaks" << endl;
15 }
16};
17 
18int main() {
19 Animal* a = new Dog();
20 a->speak();
21 delete a;
22 return 0;
23}