Quiz 6 — Answers¶
Q1 — B, private. For a class, inheritance is private by default, so the base's public
members become inaccessible to outside code (the derived class can still use them). This is
a common surprise: writing class Derived : Base silently means "implemented in terms of",
whereas class Derived : public Base means "is-a". Always write the specifier.
Q2 — False. Private members of a base are not accessible in a derived class, at all.
protected is the access level that lets derived classes reach the data. This is exactly
why protected is a deliberate choice: it couples derived classes to the base's internals.
Q3 — A, C, E. A Manager is an Employee; a Square is a Shape; a
SavingsAccount is an Account. A Car has an Engine (composition), and a Stack is
implemented using a Vector (composition, or private inheritance if you must) — neither
is an is-a.
Q4 — Base Derived ~Derived ~Base. The base sub-object is constructed first, then the
derived part; destruction runs in the reverse order, so the derived destructor runs before
the base destructor.
Q5 — Declaring describe(int) in Square hides every describe in Base: name
lookup stops at Square and never sees Base::describe(), so s.describe() finds only
the int overload and fails. Two fixes:
using Base::describe; // bring the base overloads back into view
// or
s.Base::describe(); // call the base version explicitly
Q6 — r.id = 1; is ambiguous because Router contains two Device sub-objects —
one through Networked, one through Powered — and the compiler cannot choose. The
feature that shares a single base is virtual inheritance (struct Networked : virtual
Device), which costs an indirection per object and complicates construction.
Q7 — No, c.start() does not compile. class Car : Engine is private inheritance, so
Engine's public members are not accessible to main (they are accessible inside Car,
which is why the constructor's start() call is fine). Writing : public Engine fixes it.
Private inheritance is right when the relationship is "implemented in terms of" rather than
"is-a".
Q8 — protected means the member is accessible to the class itself and to any derived
class, but not to outside code. It is a decision because it deliberately exposes internal
data to every future derived class, which couples them to that representation; anyone
subclassing can now break the invariant. Prefer private with protected accessors unless
derived classes genuinely need the data.
Q9 — A base Vehicle holding plate_ and rate_, with Car (seats), Van (payload)
and Motorbike (engine size) deriving from it. Shared: the registration plate and the daily
rate. Must not go in the base: seats, payload or engine size — a base member that fits
one kind and not the others forces every other kind to carry a meaningless field, which is
the classic inheritance mistake.
Q10 — A Car and its Engine: inheriting Car : Engine would make a car a kind of
engine, and would break the moment a car had two engines. The right relationship is
composition — a Car owns an Engine. More generally, inherit only when the derived
type is genuinely usable everywhere the base is.
Marking. One point per question (Q3 all-or-nothing). Then read Marking Yourself.