Point72 · CS Fundamentals
Explain Encapsulation, Inheritance, and Polymorphism in C++
TrueInterview
October 7, 2026 · 1 min read
Explain encapsulation, inheritance, and polymorphism in C++ by designing a small class hierarchy with one stable public interface and at least two implementations. In your explanation, discuss access control, object lifetime, virtual dispatch, substitution, composition versus inheritance, and the pitfalls of copying or destroying objects through base types.
Constraints and Assumptions
- Use resource-safe C++ ownership rather than raw owning pointers.
- If objects are deleted through a base pointer, address the base destructor explicitly.
- Keep compile-time overloading or templates separate from runtime virtual dispatch.
- Do not expose internal representation merely to make tests convenient.
Clarifying Questions to Ask
- Is the relationship truly an is-a relationship, or would composition model it better?
- Must derived objects be copied, moved, or owned polymorphically?
- Which invariants should only the class itself be able to mutate?
What Your Answer Should Cover
- Concrete definitions of the three concepts, connected to a coherent class example.
- Virtual destructors and object slicing.
- Liskov-style substitutability, and how inheritance can break invariants.
- Trade-offs among virtual interfaces, templates, variants, and composition.
Follow-up Questions
- When would a tagged variant be preferable to a virtual hierarchy?
- How can a derived method violate substitutability even when its signature matches?
Hints (3)
Loading comments…