← Quiz: Operators and Templates: all 10 questions
Quiz: Operators and Templates · Question 6 of 10
What does this print? (code)
What does this print?
#include <iostream>
using namespace std;
struct Money {
int rupees;
Money(int r) : rupees(r) {}
Money operator+(const Money &o) const { return Money(rupees + o.rupees); }
bool operator<(const Money &o) const { return rupees < o.rupees; }
};
int main() {
Money a(150), b(275);
Money c = a + b;
cout << c.rupees << " " << (a < b) << " " << (c < a) << "\n";
return 0;
}
- A.425 0 1
- B.425 1 0
- C.150275 1 0
- D.425 1 1
Answer & explanation
Correct answer: 425 1 0
a + b calls a.operator+(b), giving 425; 150 < 275 is true (1) and 425 < 150 is false (0).
Can you get the rest right without peeking?
Take the timed Quiz: Operators and Templates quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →