Constructors and Destructors in C++
Constructors and destructors are special member functions in C++ that are used for initializing and cleaning up objects, respectively. They play a crucial role in the lifecycle of an object.
Constructor
A constructor is a special member function that is automatically called when an object is created. It is used to initialize the object. Constructors have the same name as the class and do not have a return type.
Types of Constructors
- Default Constructor: A constructor with no arguments.
- Parameterized Constructor: A constructor that takes arguments.
- Copy Constructor: A constructor that creates a new object as a copy of an existing object.
Default Constructor
Example:
#include <iostream> class Car { public: Car() { // Default constructor std::cout << "Default Constructor Called: Car object created." << std::endl; } }; int main() { Car car1; // Default constructor is called return 0; }
Output:
Default Constructor Called: Car object created.
Parameterized Constructor
Example:
#include <iostream> class Car { public: std::string brand; int speed; Car(std::string b, int s) { // Parameterized constructor brand = b; speed = s; } void display() { std::cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << std::endl; } }; int main() { Car car1("Toyota", 120); // Parameterized constructor is called car1.display(); return 0; }
Output:
Brand: Toyota, Speed: 120 km/h
Copy Constructor
Example:
#include <iostream> class Car { public: std::string brand; int speed; Car(std::string b, int s) { brand = b; speed = s; } Car(const Car &c) { // Copy constructor brand = c.brand; speed = c.speed; } void display() { std::cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << std::endl; } }; int main() { Car car1("Honda", 150); Car car2 = car1; // Copy constructor is called car2.display(); return 0; }
Output:
Brand: Honda, Speed: 150 km/h
Destructor
A destructor is a special member function that is automatically called when an object goes out of scope or is explicitly deleted. Its purpose is to release resources or perform cleanup tasks. A destructor has the same name as the class but is prefixed with a tilde (~).
Example:
#include <iostream> class Car { public: Car() { std::cout << "Constructor Called: Car object created." << std::endl; } ~Car() { // Destructor std::cout << "Destructor Called: Car object destroyed." << std::endl; } }; int main() { Car car1; // Constructor is called return 0; // Destructor is called automatically }
Output:
Constructor Called: Car object created. Destructor Called: Car object destroyed.
Summary
Constructors and destructors ensure proper initialization and cleanup of objects in C++. Here is a quick overview:
- Default Constructor: Initializes an object with default values.
- Parameterized Constructor: Initializes an object with specific values provided as arguments.
- Copy Constructor: Creates a new object as a copy of an existing object.
- Destructor: Cleans up resources when an object is destroyed.