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
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() { cout << “Name: ” << student1.name << endl; // Default value: empty string 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() { } |
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() { |
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 // Parameterized constructor with one argument // Parameterized constructor with two arguments void display() { int main() { student1.display(); // Output: Name: Unknown, Age: 0 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) } }; |