Question 4
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
struct Discount {
virtual int apply(int price) const = 0;
virtual ~Discount() = default;
};
int Discount::apply(int price) const { return price - price / 10; } // default: 10% off
struct Festival : Discount {
int apply(int price) const override { return Discount::apply(price) - 50; }
};
struct NoDiscount : Discount {
int apply(int price) const override { return price; }
};
int main() {
Festival f;
NoDiscount n;
const Discount* d[] = {&f, &n};
for (auto* x : d) std::cout << x->apply(1000) << ' ';
std::cout << '\n';
return 0;
}
- A 850 1000 Correct
- B 900 1000
- C 950 1000
- D 850 900
Correct Answer: Option A
Festival reuses the pure virtual function's body (900) and subtracts 50; NoDiscount ignores the default.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!