Question 5
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
class Rupees {
long paise_;
public:
explicit Rupees(long r) : paise_(r * 100) {}
long paise() const { return paise_; }
};
void pay(Rupees amount) { std::cout << amount.paise() << '\n'; }
int main() {
pay(Rupees(250));
pay(250);
return 0;
}
- A long cannot be used for money
- B The constructor is explicit, so 250 does not convert to Rupees implicitly; write pay(Rupees(250)) Correct
- C pay must take a reference
- D paise must be public
Correct Answer: Option B
That is the point of the strong type: a bare number could be paise, rupees or a quantity, so the caller must say which.
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!