What is a Constructor in C++?
Understand what a constructor is in C++, how overloading and initializer lists work, and the difference between copy and move constructors.
Expected Interview Answer
A constructor is a special member function that automatically runs when an object is created, initializing its data members so the object starts in a valid state.
A constructor shares the class's name, has no return type, and can be overloaded to accept different parameter lists. C++ provides a default constructor (no arguments) if none is defined, but defining any constructor suppresses the compiler-generated default. Constructors can use member initializer lists, which initialize members directly rather than assigning them in the body, which is more efficient and required for const or reference members. Special constructors include the copy constructor (initializes from another object of the same type) and the move constructor (transfers resources from a temporary, avoiding a copy). Constructors work hand-in-hand with destructors under RAII to guarantee resources are acquired and released deterministically.
- Guarantees objects start in a valid, initialized state
- Supports overloading for flexible object creation
- Member initializer lists enable efficient, const-safe initialization
- Move constructors avoid unnecessary deep copies
- Forms the acquisition half of the RAII pattern
AI Mentor Explanation
A constructor is like the pre-match ritual where a new batsman walks in, straps on pads, and takes guard before facing a single ball. Different situations call different setups — a night game might need extra sightscreen adjustment, mirroring how overloaded constructors handle different starting parameters.
Step-by-Step Explanation
Step 1
Match the class name
The constructor's name must exactly match the class name and has no return type, not even `void`.
Step 2
Runs automatically
It executes the moment an object is created, whether on the stack, heap, or as a member of another object.
Step 3
Use member initializer lists
Initialize members in the `: member(value)` list rather than assigning in the body for efficiency and to support const/reference members.
Step 4
Overload as needed
Define multiple constructors with different parameter lists to support different creation scenarios.
Step 5
Understand copy and move
The copy constructor initializes from an lvalue of the same type; the move constructor initializes from an rvalue, transferring resources instead of copying.
Step 6
Pair with RAII
Acquire resources (memory, file handles) in the constructor so the destructor can reliably release them.
What Interviewer Expects
- Knows a constructor shares the class name and has no return type
- Understands member initializer lists vs body assignment
- Can explain the difference between copy and move constructors
- Knows the compiler-generated default constructor and when it's suppressed
- Connects constructors to RAII resource acquisition
Common Mistakes
- Giving the constructor a return type like `void`
- Assigning members in the body instead of the initializer list
- Forgetting that defining any constructor removes the implicit default
- Confusing the copy constructor with the assignment operator
- Not marking single-argument constructors `explicit` when implicit conversion is unwanted
Best Answer (HR Friendly)
“A constructor is a special piece of code that runs automatically when a new object is created, setting it up with valid starting values, similar to how a new hire goes through onboarding before starting real work. It ensures every object begins its life properly configured.”
Code Example
#include <iostream>
#include <string>
class Account {
private:
std::string owner;
double balance;
public:
// Overloaded constructor with member initializer list
Account(std::string name, double initial)
: owner(std::move(name)), balance(initial) {}
explicit Account(std::string name) : owner(std::move(name)), balance(0.0) {}
void print() const {
std::cout << owner << ": " << balance << "\n";
}
};
int main() {
Account a("Priya", 500.0);
Account b("Sam");
a.print(); // Priya: 500
b.print(); // Sam: 0
return 0;
}Follow-up Questions
- What is the difference between a copy constructor and a move constructor?
- When is the compiler-generated default constructor suppressed?
- Why prefer a member initializer list over assignment in the constructor body?
- What does the `explicit` keyword do on a constructor?
- What is constructor delegation in C++11 and later?
MCQ Practice
1. What is the return type of a constructor?
Constructors have no return type at all, not even `void` — this is what distinguishes them from regular member functions.
2. When does a constructor execute?
A constructor runs automatically the moment an object is instantiated, ensuring it starts in a valid state.
3. What is the main advantage of a member initializer list over assignment in the constructor body?
Member initializer lists initialize members directly at construction, which is more efficient and mandatory for const or reference members.
Flash Cards
What is a constructor's return type? — None — constructors have no return type, not even void.
When does a constructor run? — Automatically at the moment an object is created.
What does a move constructor do? — Transfers resources from a temporary (rvalue) instead of copying them.
Why use a member initializer list? — It initializes members directly and is required for const and reference members, and is more efficient than body assignment.