Interview Questions
Classes and Objects · Study material
C++ Classes and Objects – Interview Questions
1. What is a class in C++?
A user-defined type that groups data members and member functions, with access control over who may use them.
2. How do you define a member function outside the class?
Declare it inside the class, then define it with the class name and scope resolution operator: ReturnType ClassName::name(params) { … }.
3. Are member functions defined inside the class inline?
Yes, implicitly – which allows the class definition to live in a header included by many files.
4. What is a const member function?
A member function marked const after its parameter list; it cannot modify non-mutable data members and is the only kind callable on const objects.
5. What is the mutable keyword?
It allows a data member to be modified even in const member functions – used for caches, counters and mutexes.
6. What is the this pointer and what is its type?
A pointer to the object the member function was called on. In a member of class X it is X* (const X* in a const member function).
7. Why return *this by reference?
To allow method chaining (obj.a().b().c()) on the same object; returning by value would chain on copies.
8. What is a static data member?
A member shared by all objects of the class, with one storage location for the whole program.
9. How do you initialize a static data member?
In C++17 and later, static inline int count = 0; inside the class; constants as static constexpr. Before C++17, define it once in a .cpp file: int X::count = 0;.
10. Can a static member function access non-static members?
Not directly, because it has no this pointer. It can access them through an object passed to it.
11. What is the size of an empty class and why?
At least 1 byte, so that two distinct objects have different addresses.
12. Do member functions take space in each object?
No. Code exists once; only non-static data members, padding and a possible vptr occupy object storage.
13. What is a nested class?
A class declared inside another class. It is a member of the enclosing class, follows its access specifiers, and can access the enclosing class's private members.
14. What is a friend class?
A class granted access to another class's private and protected members by a friend class declaration. Friendship is neither inherited nor transitive.
15. What is an aggregate?
An array or a class with no user-declared constructors, no private or protected non-static data members, no virtual functions and no virtual or private base classes; it can be brace-initialized member by member.
16. What are designated initializers?
A C++20 syntax for aggregates that names the members: Point p{.x = 1, .y = 2};. Designators must follow declaration order.
17. What does auto operator<=>(const T&) const = default; do?
Generates three-way comparison (and with it ==, !=, <, <=, >, >=) comparing members in declaration order.
18. What is the difference between an object on the stack and on the heap?
A stack (automatic) object is destroyed automatically at the end of its scope; a heap object created with new lives until deleted – ideally owned by a smart pointer.
19. When is a static local object constructed?
The first time control passes through its declaration (thread-safely since C++11), and it is destroyed after main returns.
20. What is the difference between a class declaration and a definition?
class X; is a declaration (forward declaration) that allows pointers and references to X; the definition with the member list is needed to create objects or access members.