Constructors and Member Initializer Lists
Initialize members cleanly using member initializer lists.
Constructors and Member Initializer Lists is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a Constructor?
A constructor is a special method that runs when an object is created. It has the same name as the class and no return type.
class Point {
public:
Point() { // default constructor
x_ = 0;
y_ = 0;
}
private:
int x_, y_;
};Parameterized Constructors
Constructors can take parameters and you can have several overloads.
class Point {
int x_, y_;
public:
Point() : x_(0), y_(0) {}
Point(int x, int y) : x_(x), y_(y) {}
};Member Initializer Lists
The : member(value) syntax before the body is a member initializer list. Members are initialized here — not assigned later inside the body.
class Person {
std::string name_;
int age_;
public:
Person(std::string n, int a) : name_(std::move(n)), age_(a) {}
};Why Use Initializer Lists?
Members are constructed exactly once with the right value. Without the list, members are default-constructed and then assigned — wasteful for non-trivial types.
Initialization Order
Members are initialized in the order they are declared in the class — not the order in the initializer list. Match the order to avoid confusion.
In-Class Default Initializers (C++11)
You can give members default values directly in the class body. Useful when most constructors share the same defaults.
class Buffer {
size_t size_ = 0;
char* data_ = nullptr;
public:
Buffer() = default;
Buffer(size_t n) : size_(n), data_(new char[n]) {}
};= default and = delete
Ask the compiler to provide or remove a special member:
class Widget {
public:
Widget() = default; // compiler-generated default
Widget(const Widget&) = delete; // disable copy
};Delegating Constructors (C++11)
A constructor can call another constructor of the same class to share initialization logic.
class Box {
int w_, h_, d_;
public:
Box(int w, int h, int d) : w_(w), h_(h), d_(d) {}
Box() : Box(1, 1, 1) {} // delegates to the 3-arg ctor
};Explicit Constructors
Single-argument constructors can be used in implicit conversions. Mark them explicit to prevent surprise conversions.
class Vector {
public:
explicit Vector(size_t n);
};
Vector v = 10; // ERROR: explicit prevents this
Vector v(10); // OKDestructors
The destructor ~ClassName() runs when an object is destroyed. Use it to release resources (files, sockets, raw memory).
The Rule of Five (Sneak Peek)
If you write any of the special member functions (destructor, copy constructor, copy assignment, move constructor, move assignment), think about all five. Covered in the move semantics course.
Quick Check
What determines the order in which class members are initialized?
Recap
Use constructors to set object state. Prefer member initializer lists over body assignments. Mark single-arg constructors explicit and use = default / = delete to communicate intent.
Frequently asked questions
Is the “Constructors and Member Initializer Lists” lesson free?
Yes — the full text of “Constructors and Member Initializer Lists” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Constructors and Member Initializer Lists”?
Initialize members cleanly using member initializer lists. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Constructors and Member Initializer Lists” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- struct vs class When to Use Each
- Member Variables and Methods
- Constructors and Member Initializer Lists
- The this Pointer and Encapsulation