C++ Constructors β Types, Overloading, and Delegation Explained
Introduction β Why Constructors Are Crucial in C++
In C++ programming, a constructor is a special member function that gets invoked automatically when an object is created. It initializes the objectβs state and sets up necessary resources. Without proper constructors, your class objects may remain uninitialized, leading to undefined behavior.
In this guide, you’ll learn:
- What constructors are and how they work
- Different types of constructors
- Constructor overloading and delegation
- Syntax, rules, examples, and best practices
What Is a Constructor?
A constructor:
- Has the same name as the class
- No return type, not even
void - Automatically called when an object is created
Code Examples β With Output & Explanation
Example 1: Default Constructor
#include <iostream>
using namespace std;
class Person {
public:
Person() {
cout << "Default constructor called!" << endl;
}
};
int main() {
Person p1;
return 0;
}
Output:
Default constructor called!
Explanation:
Person()is the default constructor- It runs automatically when
p1is created
Example 2: Parameterized Constructor
#include <iostream>
using namespace std;
class Person {
string name;
public:
Person(string n) {
name = n;
}
void display() {
cout << "Name: " << name << endl;
}
};
int main() {
Person p("Alice");
p.display();
return 0;
}
Output:
Name: Alice
Example 3: Constructor Overloading
class Rectangle {
int width, height;
public:
Rectangle() {
width = height = 0;
}
Rectangle(int w, int h) {
width = w;
height = h;
}
int area() { return width * height; }
};
Usage:
Rectangle r1, r2(4, 5);
cout << r1.area(); // 0
cout << r2.area(); // 20
Example 4: Constructor Delegation (C++11)
class Student {
int age;
public:
Student() : Student(18) {} // delegates to another constructor
Student(int a) { age = a; }
void show() { cout << "Age: " << age << endl; }
};
int main() {
Student s1;
Student s2(21);
s1.show(); // Age: 18
s2.show(); // Age: 21
}
Constructor Types in C++
| Type | Description |
|---|---|
| Default Constructor | Takes no parameters |
| Parameterized | Takes one or more arguments |
| Copy Constructor | Initializes an object from another existing object |
| Move Constructor | Transfers ownership from temporary objects (C++11+) |
| Delegating Constructor | One constructor calls another constructor in the same class |
| Explicit Constructor | Prevents implicit conversions |
Best Practices & Tips
Best Practice: Always initialize member variables using constructor initializer lists for better performance.
Tip: Use explicit keyword to prevent undesired implicit conversions:
explicit Person(string name);
Pitfall: Don’t forget to define a default constructor if you have other parameterized ones (and you need default construction).
Use Cases for Constructors
Initialization: Assign default values to class members
Overloading: Support multiple ways to create objects
Object Cloning: Use copy constructors to duplicate objects
Safe Resource Setup: Constructors can allocate memory or open files
Summary β Recap & Next Steps
Key Takeaways:
- Constructors initialize objects automatically
- Support overloading and delegation
- Use initializer lists for performance
- Use
explicitto avoid unwanted conversions
Real-World Relevance:
Used in data models, GUI frameworks, simulations, object factories, and configuration loaders.
Next Steps:
- Learn about Destructors
- Explore Copy vs Move Constructor
FAQ β C++ Constructors
Is a constructor mandatory in C++?
No. If you donβt define one, the compiler provides a default constructor.
Can constructors be overloaded?
Yes. You can define multiple constructors with different parameter sets.
Can a constructor return a value?
No. Constructors do not have a return typeβnot even void.
What is a delegating constructor?
Itβs a constructor that calls another constructor of the same class using : initializer syntax.
Can a constructor be private?
Yes. This is often used in Singleton design pattern to restrict object creation.
Share Now :
