Interview Questions
Abstraction · Study material
C++ Abstraction – Interview Questions
1. What is abstraction?
Exposing only the essential operations of something and hiding its implementation details.
2. What is the difference between abstraction and encapsulation?
Abstraction decides what to show (the interface); encapsulation hides and protects the internals (bundling data with functions and restricting access).
3. How do you achieve abstraction in C++?
Classes with access specifiers, abstract classes/interfaces, header files, templates and concepts, and functions.
4. What is data abstraction?
Representing data by its operations rather than its storage – for example a Date class with add_days() instead of three public ints.
5. What is an abstract data type?
A type defined by its operations and their behaviour, independent of representation: stack, queue, map, set.
6. What is control abstraction?
Hiding sequences of steps behind a function or construct – functions, loops, algorithms like std::sort.
7. What is an interface in C++?
C++ has no interface keyword; an interface is a class with only pure virtual functions and a virtual destructor.
8. What does "program to an interface" mean?
Depend on abstract types (or concepts) rather than concrete classes, so implementations can be swapped.
9. What is the pimpl idiom?
Keeping private members in a separate Impl struct accessed through a pointer, so the header stays stable and users need not recompile when the implementation changes.
10. What are the costs of pimpl?
A heap allocation per object, an extra indirection per access and some boilerplate (destructor and moves defined in the source file).
11. How is RAII a form of abstraction?
It hides the need to release resources: acquisition happens in the constructor, release in the destructor, automatically on every path.
12. What is a leaky abstraction?
An abstraction whose hidden details still affect users, forcing them to understand the implementation.
13. What is the zero-overhead principle?
Abstractions you do not use cost nothing, and those you use are as efficient as hand-written code.
14. Is a class with all public data members an abstraction?
Only a weak one: it names a concept but exposes its representation, so users depend on it.
15. How do templates provide abstraction?
They write algorithms and containers once, independent of the element type, with static dispatch.
16. What do C++20 concepts add?
Named, checked requirements on template parameters – an explicit compile-time interface with clear error messages.
17. Why are getters and setters for every field a poor abstraction?
They expose the representation one field at a time, letting callers depend on it and bypass the class's rules.
18. Can abstraction hurt performance?
Run-time abstraction (virtual calls, pimpl) has small costs; compile-time abstraction (templates, inline functions) usually has none.
19. What is the dependency inversion principle?
High-level modules should depend on abstractions, not on low-level modules; both depend on the abstraction.
20. Give a real-world C++ library example of abstraction.
Iterators: algorithms like std::find work on vectors, lists, arrays and streams because they use the iterator abstraction, not the container.