Puzzle 22 | The Phantom Spaceship |
| #include <iostream> |
| #include <string> |
| |
| struct GameObject |
| { |
| GameObject() { std::cout << "Created a " << getType() << "\n"; } |
| void render() const { std::cout << "Rendered a " << getType() << "\n"; } |
| virtual std::string getType() const { return "GameObject"; } |
| }; |
| |
| class Spaceship : public GameObject |
| { |
| std::string getType() const override { return "Spaceship"; } |
| }; |
| |
| void display(const GameObject &gameObject) { gameObject.render(); } |
| |
| int main() |
| { |
| GameObject gameObject; |
| Spaceship spaceship; |
| display(gameObject); |
| display(spaceship); |
| } |
Guess the Output | |
---|---|
Try to guess what the output is before moving to the next ... |
Get C++ Brain Teasers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.