We are going to explain:
- What is a constructor?
- Can a class have multiple constructors?
- What is a copy constructor?
Let's break it down.
1. What is a Constructor?
A constructor is a special member function of a class that:
- Has the same name as the class.
- Is automatically called when an object of the class is created.
- Initializes the object's data members (variables).
- Has no return type (not even
void).
- Can be overloaded (multiple constructors with different parameters).
Example in C++:
class MyClass {
int x;
public:
MyClass() { x = 0; } // Default constructor (no parameters)
MyClass(int val) { x = val; } // Parameterized constructor
};
2. Can a Class Have Multiple Constructors?
Yes. This is called constructor overloading. A class can have multiple constructors with different parameter lists (different signatures). The appropriate constructor is called based on the arguments passed during object creation.
Example:
class Box {
double length;
public:
Box() { length = 1.0; } // Constructor 1
Box(double l) { length = l; } // Constructor 2
Box(double l, double w) { ... } // Constructor 3 (if more parameters)
};
Note:
- Compilers provide a default constructor (no parameters) if no constructors are defined explicitly—but only if no other constructors are defined.
- If you define any constructor, the default one is not automatically added (unless explicitly defined).
3. What is a Copy Constructor?
A copy constructor is a constructor that:
- Takes a reference to an object of the same class as its parameter.
- Creates a new object as a copy of the existing object.
- Is used for:
- Initializing a new object from an existing one.
- Passing objects by value to functions.
- Returning objects by value from functions.
Syntax:
ClassName(const ClassName& obj) {
// Copy data members from obj
}
Example:
class Student {
string name;
int age;
public:
Student(const Student& s) { // Copy constructor
name = s.name;
age = s.age;
}
};
When is it called?
Student s1("Alice", 20);
Student s2 = s1; // Copy constructor invoked
Student s3(s1); // Copy constructor invoked
Why is it important?
- Without a custom copy constructor, C++ performs a shallow copy (copies member values directly).
- For classes with raw pointers or dynamic resources (e.g.,
int* data), a shallow copy can cause multiple objects pointing to the same memory (dangling pointers, double-free errors).
- A deep copy (custom copy constructor) duplicates the dynamic resources safely.
Example of Deep Copy:
class DynamicArray {
int* arr;
int size;
public:
DynamicArray(int s) : size(s) { arr = new int[s]; }
~DynamicArray() { delete[] arr; }
// Copy constructor for deep copy
DynamicArray(const DynamicArray& other) : size(other.size) {
arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = other.arr[i];
}
}
};
Key Takeaways:
- Constructors initialize objects; they can be overloaded.
- Copy constructors create copies of existing objects (critical for deep copying).
- If no copy constructor is defined, C++ generates a default one (shallow copy).
- Always define a copy constructor (and destructor, copy assignment operator) for classes managing resources—this is part of the Rule of Three.