← Back to Question List
C++ Quiz #36
Object Slicing
The programmer expects this to print "Cat meows", but it prints "Animal speaks" instead. What went wrong?
📄 Code
1#include <iostream>2using namespace std;34class Animal {5public:6 virtual void speak() {7 cout << "Animal speaks" << endl;8 }9};1011class Cat : public Animal {12public:13 void speak() override {14 cout << "Cat meows" << endl;15 }16};1718void makeSound(Animal a) { // pass by value19 a.speak();20}2122int main() {23 Cat c;24 makeSound(c);25 return 0;26}