LearnFrenzy
Sign in Take a free test

Abstraction · Find the Error · Question 5

Error: passing a raw number to a strong type Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links