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 #40

Accidental Method Signature Mismatch

mediumPolymorphism

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;
3 
4class Base {
5public:
6 virtual void process(int x) {
7 cout << "Base: " << x << endl;
8 }
9 virtual ~Base() {}
10};
11 
12class Derived : public Base {
13public:
14 void process(int x) const { // note: const added
15 cout << "Derived: " << x << endl;
16 }
17};
18 
19int main() {
20 Base* b = new Derived();
21 b->process(42);
22 delete b;
23 return 0;
24}