← Back to Question List
C++ Quiz #40
Accidental Method Signature Mismatch
The programmer expects this to print "Derived: 42", but it prints "Base: 42" instead. What is the bug?
📄 Code
1#include <iostream>2using namespace std;34class Base {5public:6 virtual void process(int x) {7 cout << "Base: " << x << endl;8 }9 virtual ~Base() {}10};1112class Derived : public Base {13public:14 void process(int x) const { // note: const added15 cout << "Derived: " << x << endl;16 }17};1819int main() {20 Base* b = new Derived();21 b->process(42);22 delete b;23 return 0;24}