C++ — Constructors
Default constructor
#include <iostream>
using namespace std;
class Point {
public:
double x, y;
// Default constructor
Point() : x(0), y(0) {
cout << "Default constructor called" << endl;
}
};
int main() {
Point p; // Calls default constructor
cout << "(" << p.x << ", " << p.y << ")" << endl;
return 0;
}
Parameterized constructor
#include <iostream>
using namespace std;
class Rectangle {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() const { return width * height; }
};
int main() {
Rectangle r(5.0, 3.0);
cout << "Area: " << r.area() << endl;
return 0;
}
Member initializer list
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
const string name; // Must be initialized in initializer list
const int id; // Must be initialized in initializer list
int age;
public:
Person(string n, int i, int a)
: name(n), id(i), age(a) // Order matches declaration order
{
cout << "Created: " << name << endl;
}
void print() const {
cout << name << " (ID: " << id << ", Age: " << age << ")" << endl;
}
};
int main() {
Person p("Alice", 1001, 30);
p.print();
return 0;
}
Delegating constructors
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int year, month, day;
public:
// Main constructor
Date(int y, int m, int d) : year(y), month(m), day(d) {
cout << "Full constructor" << endl;
}
// Delegates to main constructor
Date() : Date(2026, 1, 1) {
cout << "Default delegation" << endl;
}
// Delegates with partial defaults
Date(int y) : Date(y, 1, 1) {
cout << "Year-only delegation" << endl;
}
void print() const {
cout << year << "-" << month << "-" << day << endl;
}
};
int main() {
Date d1; // Default
Date d2(2025); // Year-only
Date d3(2024, 6, 15); // Full
d1.print();
d2.print();
d3.print();
return 0;
}
Copy constructor
#include <iostream>
using namespace std;
class Widget {
private:
int *data;
public:
Widget(int value) : data(new int(value)) {
cout << "Constructed with value " << value << endl;
}
Widget(const Widget &other) : data(new int(*other.data)) {
cout << "Copy constructed with value " << *data << endl;
}
~Widget() {
delete data;
}
int getValue() const { return *data; }
};
int main() {
Widget w1(42);
Widget w2 = w1; // Copy constructor
Widget w3(w1); // Also copy constructor
cout << w1.getValue() << " " << w2.getValue() << " " << w3.getValue() << endl;
return 0;
}
Move constructor (C++11)
#include <iostream>
#include <utility>
using namespace std;
class Buffer {
private:
int *data;
size_t size;
public:
Buffer(size_t s) : data(new int[s]), size(s) {
cout << "Allocated " << size << " elements" << endl;
}
// Move constructor — steals resources
Buffer(Buffer &&other) noexcept
: data(other.data), size(other.size)
{
other.data = nullptr;
other.size = 0;
cout << "Move constructed" << endl;
}
~Buffer() {
delete[] data;
}
size_t getSize() const { return size; }
};
int main() {
Buffer b1(100);
Buffer b2 = std::move(b1); // Move, not copy
cout << "b1 size: " << b1.getSize() << endl; // 0
cout << "b2 size: " << b2.getSize() << endl; // 100
return 0;
}
Explicit constructors
#include <iostream>
using namespace std;
class Meter {
private:
double value;
public:
explicit Meter(double v) : value(v) {}
double getValue() const { return value; }
};
void printLength(Meter m) {
cout << "Length: " << m.getValue() << "m" << endl;
}
int main() {
Meter m1(5.0); // OK
// Meter m2 = 5.0; // Error: implicit conversion blocked
Meter m3 = Meter(5.0); // OK: explicit construction
printLength(Meter(5.0)); // OK
// printLength(5.0); // Error: implicit conversion blocked
return 0;
}
Aggregate initialization
#include <iostream>
using namespace std;
struct Point {
double x, y;
};
struct Color {
int r, g, b;
};
int main() {
Point p = {3.0, 4.0};
Point p2{1.0, 2.0}; // Brace initialization
Color red = {255, 0, 0};
Color green{0, 255, 0};
cout << "Point: (" << p.x << ", " << p.y << ")" << endl;
cout << "Red: (" << red.r << ", " << red.g << ", " << red.b << ")" << endl;
return 0;
}
Brace elision
#include <iostream>
using namespace std;
struct Inner {
int a, b;
};
struct Outer {
Inner inner;
int c;
};
int main() {
// Nested aggregate initialization
Outer o = {{1, 2}, 3};
// Or with brace elision:
Outer o2 = {1, 2, 3};
cout << o.inner.a << ", " << o.inner.b << ", " << o.c << endl;
return 0;
}
Mini Practice
Write C++ code that:
- Creates a class with both default and parameterized constructors
- Uses a member initializer list for const members
- Implements a move constructor
- Demonstrates
explicitto prevent implicit conversions
Up Next
In the next lesson, you'll learn about Inheritance — deriving classes and code reuse.
Related Topics
Frequently Asked Questions about Constructors
What is Constructors in C++?
Constructors is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Constructors?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Constructors.
Why is Constructors important in C++?
Constructors is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.