C++ Constructor

In C++, a constructor is a special member function of a class that is automatically called when an object of that class is created. Its primary purpose is to initialize objects of the class, i.e., it sets the initial state of the object.

Key Points of Constructors:

1. Same name as the class: The constructor has the same name as the class it belongs to.

2. No return type: Constructors do not have a return type, not even void.

3. Automatically called: They are invoked automatically when an object is created.

4. Overloading: C++ allows constructor overloading, meaning you can have multiple constructors with different parameter lists.

Types of Constructors

1. Default Constructor

A constructor that takes no arguments. If no constructor is explicitly defined, C++ provides a default constructor automatically.

Syntax:


class MyClassExample{
public:
    MyClassExample() {  // Default constructor
        // Initialization code here
    }
};

Example:


#include <iostream>
using namespace std;

#include 
using namespace std;

class Employee {
public:
    int age;
    // Default constructor
    Employee() {
        age = 18;  // Initialize age to 18 by default
    }
    
    void display() {
        cout << "Employee's age = " << age << endl;
    }
};

int main() {
    Employee emp;  // Calls default constructor
    emp.display();  // Output: age = 18
    return 0;
}

Output:

Employee's age = 18

2. Parameterized Constructor

A constructor that takes parameters to initialize the object with specific values.

Syntax:


class MyClassExample {
public:
    int a;
    MyClassExample(int val) {  // Parameterized constructor
        a = val;
    }
};

Example:


#include <iostream>
using namespace std;

class Employee {
public:
    int age;
    // parameterized constructor
    Employee(int val) {
        age = val;  // Initialize age to 18 by default
    }
    
    void display() {
        cout << "Employee's age = " << age << endl;
    }
};

int main() {
    Employee emp(35);  // Calls parameterized constructor with value 35
    emp.display();  // Output: age = 35
    return 0;
}

Output:

Employee's age = 35

3. Copy Constructor

A constructor that creates a new object as a copy of an existing object. It takes a reference to another object of the same class as its parameter.

Syntax:


class MyClassExample {
public:
    int a;
    MyClassExample(const MyClassExample &obj) {  // Copy constructor
        a = obj.a;
    }
};

Example:


#include <iostream>
using namespace std;

class Employee {
public:
    int age;
    // parameterized constructor
    Employee(int val) {
        age = val;  // Initialize age to 18 by default
    }
    // Copy constructor
    Employee(const Employee &obj) {
        age = obj.age;  // Copy the value of age from the existing object
    }
    void display() {
        cout << "Employee's age = " << age << endl;
    }
};

int main() {
    Employee emp(35);  // Calls parameterized constructor with value 35
    Employee emp2 = emp;  // Calls copy constructor
    emp.display();  // Output: age = 35
    emp2.display();  // Output: age = 35
    return 0;
}

Output:

Employee's age = 35
Employee's age = 35