Constructor In C++

In C++ Object-Oriented Programming (OOP), a constructor is a special member function of a class that is automatically invoked when an object of that class is created. Its purpose is to initialize the object’s data members and ensure that the object begins in a valid state. Here are the key points about constructors in C++:

Characteristics of Constructors in C++

Here are the key characteristics of constructors in C++

  • Same Name as Class: The constructor’s name must be the same as the class.
  • Automatic Invocation: Automatically called when an object is created.
  • No Return Type: Constructors do not have a return type, not even void.

Types of Constructors in C++

There are various types of constructors, let explain some important of these

Constructor in C++ and its types

1. Implicit Constructor

  • An implicit constructor is a constructor that is automatically provided by the compiler if no constructor is defined by the programmer.
  • The implicit constructor is not always a default constructor. It could be a constructor with parameters if the class is designed in such a way that objects can be implicitly constructed from given values (for example, implicit conversion).
Automatic Initialization: The constructor initializes members to default values (e.g., integers to 0, strings to empty, pointers to nullptr).
#include <iostream>
using namespace std;class Student {
public:
string name;
int age;
};

int main() {
// Implicit constructor is called automatically
Student student1; // Default constructor is called by the compiler

cout << “Name: ” << student1.name << endl; // Default value: empty string
cout << “Age: ” << student1.age << endl; // Default value: 0

return 0;
}

2. Default Constructor

A default constructor is a constructor that takes no arguments. It is used to initialize an object when no initial values are provided.

class Student {
public:
string name;
int age;// Default constructor
Student() {
name = “Unknown”;
age = 0;
}
};

int main() {
// Creating objects using different constructors
Student student1; // Default constructor

}

3. Parameterized Constructor

A parameterized constructor takes arguments and uses them to initialize an object with specific values.

class Student {
public:
string name;
int age;// Parameterized constructor
Student(string n, int a) {
name = n;
age = a;
}
};

int main() {
Student s1(“Alice”, 21); // Calls parameterized constructor
return 0;
}

4. Constructor Overloading

Constructor Overloading occurs when a class has multiple constructors with the same name (the same as the class) but with different parameter lists. This allows you to create objects in different ways based on the arguments passed during object creation.

#include <iostream>
using namespace std;class Student {
public:
string name;
int age;

// Default constructor
Student() {
name = “Unknown”;
age = 0;
cout << “Default Constructor called!” << endl;
}

// Parameterized constructor with one argument
Student(string n) {
name = n;
age = 18; // Default age
cout << “Constructor with one argument called!” << endl;
}

// Parameterized constructor with two arguments
Student(string n, int a) {
name = n;
age = a;
cout << “Constructor with two arguments called!” << endl;
}

void display() {
cout << “Name: ” << name << “, Age: ” << age << endl;
}
};

int main() {
// Creating objects using different constructors
Student student1; // Default constructor
Student student2(“Alice”); // Constructor with one argument
Student student3(“Bob”, 25); // Constructor with two arguments

student1.display(); // Output: Name: Unknown, Age: 0
student2.display(); // Output: Name: Alice, Age: 18
student3.display(); // Output: Name: Bob, Age: 25

return 0;
}

Destructors vs Constructors

While a constructor initializes an object when it is created, a destructor is a special member function that is automatically called when an object goes out of scope or is explicitly deleted. Destructors are used to clean up resources (e.g., memory, file handles) allocated by the object.

class MyClass {
public:
MyClass() {
// Constructor logic
}~MyClass() {
// Destructor logic (clean up)
}
};