Skip to content

Quiz 8 — Answers

Back to the quiz


Q1 — B, a pure virtual function. A plain virtual function has a definition, so the class is still concrete. Only = 0 (a pure virtual function) makes the type abstract and therefore un-instantiable.

Q2 — True. An abstract class can hold complete, shared code that every derived class reuses. Only the pure virtual functions have no body. That is the difference between an abstract base class (shared code + a promise) and a pure interface (a promise only).

Q3 — A, B, D. An interface is pure virtual functions plus a virtual destructor (A), needs a virtual destructor so it can be deleted through, (B), and should document a contract for each operation (D). Data members for shared state (C) belong in an abstract base, not a pure interface — they couple every implementer to that choice.

Q4 — Line A fails; line B prints Summary. Line A creates a Report directly, but Report has a pure virtual title(), so it is abstract and cannot be instantiated. Line B is fine: Summary implements title(), and render() (which has a body) calls it virtually.

Q5 — Do not instantiate Report; use a concrete type:

Summary s;      // Report r; is not allowed
s.render();     // prints Summary

Keep Report as the interface — the fix is to have a derived class, not to weaken the base.

Q6 — 19. Square(3).area() = 9 and Rect(2,5).area() = 10; the loop sums 19 through the Shape* interface, without knowing either concrete type.

Q7 — A contract is what an operation promises: the conditions under which it may be called (preconditions) and what will be true afterwards (postconditions). For withdraw(amount): precondition — amount > 0 and amount <= balance(); postcondition — balance() has decreased by amount, and the balance is still non-negative. The contract is what callers rely on, and what a derived class must not weaken (Liskov).

Q8 — A variable needs a complete type — a real object with a known layout and a constructor. An abstract class has an unimplemented pure virtual function, so it has no complete object to create. But a reference or pointer to it is fine, because it never creates one; it refers to a concrete derived object. That is why an interface makes a good parameter or return type (MediaSource&) and a bad variable type.

Q9 —

class MediaSource {
public:
    virtual ~MediaSource() = default;
    virtual void open()  = 0;
    virtual void play()  = 0;
    virtual void pause() = 0;
    virtual void stop()  = 0;
};

One rule every implementation must respect: failure is reported by exception, and play before open is an error — so callers cannot silently ignore a failed open. Stating the failure rule is the point; an interface is a promise.

Q10 — Because not every source can seek. If seek lived on the main interface, every implementation would have to provide it — most by an empty or throwing stub — and callers could not tell which ones actually support it. A second interface (Seekable) keeps the main contract honest and lets callers depend only on what they need. That is the Interface Segregation Principle.


Marking. One point per question (Q3 all-or-nothing). Then read Marking Yourself.