The short answer
A constructor makes sure an object starts in a valid state every time it is created. Without one, nothing sets up the object’s data at the moment of creation, so it is left to you to remember to do it later.
C++ does not force you to write a constructor. If you do not write one, the compiler generates a default constructor. But that default knows nothing about your rules. It cannot pick a sensible starting balance for a bank account, and it cannot reject a bad value. You need a constructor when an object needs meaningful starting values or has rules it must follow.
A class without a constructor
Here is a bank account class that relies on a separate setup step:
class Account {
public:
void setup(double amount) { balance_ = amount; }
void withdraw(double amount) { balance_ -= amount; }
private:
double balance_;
};
int main() {
Account account;
account.withdraw(20.0); // setup() was never called
}
The object is only usable if the caller remembers to call setup() first. Here the caller forgot, so withdraw subtracts from a value that was never set. Reading an uninitialized double is undefined behavior, so the program has no reliable result.
The same class with a constructor
With a constructor, the setup happens as soon as the object exists:
#include <stdexcept>
class Account {
public:
Account(double opening_balance) : balance_(opening_balance) {
if (opening_balance < 0) {
throw std::invalid_argument("opening balance cannot be negative");
}
}
void withdraw(double amount) { balance_ -= amount; }
private:
double balance_;
};
int main() {
Account account(100.0); // balance_ starts at 100.0
account.withdraw(20.0); // balance_ is now 80.0
}
There is no separate setup step to forget. The constructor body also checks the rules. A negative opening balance is rejected at the moment of creation, so an invalid account never exists.
What a constructor guarantees
- Every object gets its starting state at the moment it is created.
- The rules for valid starting values live in one place, the constructor.
- Members that must be set up at creation, such as
constmembers and reference members, are initialized in the constructor’s initializer list.
Where this fits with encapsulation
Constructors work closely with encapsulation in c. When data is private, outside code cannot change it directly. The constructor becomes the controlled entry point for setting up the starting state, and the class’s member functions control every later change. Together, they keep the object consistent from start to finish.
When a default is enough
Some classes do not need a constructor at all. If every member has a sensible starting value, the compiler’s default constructor is enough. For a built-in member, a default member initializer is a lighter alternative to writing a constructor:
class Counter {
public:
void increment() { ++count_; }
int value() const { return count_; }
private:
int count_ = 0; // default member initializer: no constructor needed
};
Plain C has no constructors
Plain C has no constructors, so C programmers set up structs by hand, with an initializer or a function. Much of the extra structure that C++ adds, constructors included, is what people mean when they ask whether C++ is basically C with classes. Once you have used a constructor, the benefit is easy to see: a new object always starts in a known state, so the code that uses it does not need to check that setup happened.