Question 3
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
class Clock {
int hour_ = 9;
public:
int hour() { return hour_; }
};
int main() {
const Clock c;
std::cout << c.hour() << '\n';
return 0;
}
- A hour_ must be static
- B hour() is not marked const, so it cannot be called on a const object; declare int hour() const Correct
- C const objects cannot be created
- D cout cannot print hour
Correct Answer: Option B
Mark every member function that does not modify the object as const – it makes the class usable through const references.
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!