TRT
Sign in
easyCoreconcept

How does member lookup and construction order work when Dog inherits from Animal?

Explain base-first construction, derived-first destruction, and member lookup from derived into base with the five inheritance shapes.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstInheritance: Reusing and Extending TypesPro — members onlyReuse base class state and behavior in derived types, recognize the five inheritance shapes, and predict construction order and member lookup in small hierarchies.

A strong answer

A strong candidate frames How does member lookup and construction order work when Dog inherits from Animal? around the exact memory layout, compiler semantics, and hardware execution model covered in Inheritance: Reusing and Extending Types. Explain base-first construction, derived-first destruction, and member lookup from derived into base with the five inheritance shapes.

Dog dog; dog.eat(); prints the animal line even though Dog never defines eat. The member comes from the base. That is the whole idea: write shared state and shared functions once, then extend.

A base class is the type that supplies shared members. A derived class is the type that inherits those members and may add its own. When you write class Dog : public Animal, every Dog object embeds an Animal part followed by the Dog part. Construction builds the base part first, then the derived part. Destruction reverses that order.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Describe how a derived type reuses base members.
  • Mastery of core invariant: Distinguish single, multiple, multilevel, hierarchical, and hybrid inheritance.
  • Mastery of core invariant: Predict construction order and member lookup in a small hierarchy.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01What happens when two bases define the same member name?
  2. 02When would you choose composition over inheritance?

Not started

Sign in to save your learning progress.

Sign in to save