Quiz 5 — Operators and Static Members¶
Ten questions on making a type behave like a built-in one, and on state that belongs to the class rather than to an object.
Attempt all of them closed-book, then mark yourself against the answers.
Concepts: operator overloading · member vs free functions · operator<< ·
prefix and postfix ++ · static data and function members · strict weak ordering.
Q1. (MCQ) Which operator must be a member function?
- A.
operator+ - B.
operator== - C.
operator= - D.
operator<<
Q2. (true/false) operator< may use <= inside without any consequence, as long as
it is consistent.
Q3. (multiple-select) For which of these is a free function usually the better choice? Select all that apply.
- A. A symmetric
operator+where either operand may need conversion - B.
operator<<for output - C.
operator=for copy assignment - D.
operator[]for element access
Q4. (predict) Write the three output lines.
#include <iostream>
class Money {
public:
explicit Money(long long cents) : cents_(cents) {}
Money operator+(const Money& other) const { return Money{cents_ + other.cents_}; }
bool operator<(const Money& other) const { return cents_ < other.cents_; }
bool operator==(const Money& other) const { return cents_ == other.cents_; }
long long cents() const { return cents_; }
private:
long long cents_;
};
int main() {
Money price{1250};
Money tax{175};
Money total = price + tax;
std::cout << total.cents() << '\n';
std::cout << (price < tax) << '\n';
std::cout << (price == price) << '\n';
}
Q5. (predict) Write the three output lines.
#include <iostream>
class Ticket {
public:
Ticket() : id_(++issued_) {}
int id() const { return id_; }
static int issuedCount() { return issued_; }
private:
int id_;
static int issued_;
};
int Ticket::issued_ = 100;
int main() {
std::cout << Ticket::issuedCount() << '\n';
Ticket a;
Ticket b;
std::cout << a.id() << ' ' << b.id() << '\n';
std::cout << Ticket::issuedCount() << '\n';
}
Q6. (debug) The comparator makes std::sort misbehave. State why and fix it.
class Score {
public:
explicit Score(int points) : points_(points) {}
bool operator<(const Score& other) const { return points_ <= other.points_; }
int points() const { return points_; }
private:
int points_;
};
Q7. (analysis) A member operator+ lets m + 2 compile but blocks 2 + m.
Explain why, and say what fixes it.
Q8. (short answer) What must operator<< return, and why does it matter?
Q9. (predict) Write the two output lines.
#include <iostream>
class Counter {
public:
Counter& operator++() { ++n_; return *this; }
Counter operator++(int) { Counter old = *this; ++n_; return old; }
int n() const { return n_; }
private:
int n_ = 0;
};
int main() {
Counter c;
++c;
c++;
std::cout << c.n() << '\n';
Counter d = c++;
std::cout << c.n() << ' ' << d.n() << '\n';
}
Q10. (design) A Money type must never hold a negative amount. Decide how it is
stored and which operators it needs, and say which operator you would not provide
and why.
Scoring: mark yourself with the answers, then read the self-assessment guide.