In Object-Oriented Programming (OOP), constructors and destructors are special member functions of a class. They play a key role in managing an object's lifecycle—constructors initialize objects, while destructors clean up resources when objects are destroyed. These functions help automate memory management and ensure that objects are properly initialized and cleaned up.
A constructor is a special member function that is automatically called when an object of a class is created. The primary purpose of a constructor is to initialize the object, i.e., to assign values to its data members (attributes) when it is created. Constructors can also perform any other necessary setup or preparation tasks.
void.#include <iostream>
using namespace std;
class Car {
private:
string model;
int year;
public:
// Default constructor (no parameters)
Car() {
model = "Unknown";
year = 0;
cout << "Default Constructor Called" << endl;
}
// Parameterized constructor
Car(string m, int y) {
model = m;
year = y;
cout << "Parameterized Constructor Called" << endl;
}
// Method to display car details
void displayInfo() {
cout << "Model: " << model << ", Year: " << year << endl;
}
};
int main() {
Car car1; // Default constructor
car1.displayInfo();
Car car2("Tesla Model S", 2022); // Parameterized constructor
car2.displayInfo();
return 0;
}
Explanation:
Car() is the default constructor. It initializes the object's model to "Unknown" and year to 0.Car(string m, int y) is the parameterized constructor that allows the user to specify the model and year during object creation.car1 is created, the default constructor is invoked, and when car2 is created, the parameterized constructor is used.A destructor is a special member function that is called when an object goes out of scope or is explicitly deleted. The purpose of the destructor is to release resources that the object may have acquired during its lifetime, such as memory, file handles, or network connections. Destructors help prevent memory leaks by ensuring that resources are properly cleaned up when an object is destroyed.
~) symbol.delete is called).#include <iostream>
using namespace std;
class Car {
private:
string model;
int year;
public:
// Constructor
Car(string m, int y) {
model = m;
year = y;
cout << "Car object created: " << model << " (" << year << ")" << endl;
}
// Destructor
~Car() {
cout << "Car object destroyed: " << model << " (" << year << ")" << endl;
}
// Method to display car details
void displayInfo() {
cout << "Model: " << model << ", Year: " << year << endl;
}
};
int main() {
// Creating a car object (constructor is called)
Car car1("Honda Civic", 2021);
car1.displayInfo(); // Output car details
// When the program ends, car1 goes out of scope, and the destructor is called
return 0; // Destructor automatically called here
}
Explanation:
model and year of the car and displays a message.car1 goes out of scope at the end of the main() function, the destructor is automatically invoked.The copy constructor is a special type of constructor that is used to create a new object as a copy of an existing object. It is called when:
Car car2 = car1;).By default, C++ performs a shallow copy (bitwise copy) when creating a copy of an object. However, this might not be sufficient if the object contains dynamically allocated memory or complex resources. In such cases, you may need to implement a deep copy.
#include <iostream>
using namespace std;
class Car {
private:
string model;
int year;
public:
// Constructor
Car(string m, int y) {
model = m;
year = y;
cout << "Car created: " << model << " (" << year << ")" << endl;
}
// Copy Constructor
Car(const Car &other) {
model = other.model; // Copy model from another car
year = other.year; // Copy year from another car
cout << "Car copied: " << model << " (" << year << ")" << endl;
}
// Method to display car details
void displayInfo() {
cout << "Model: " << model << ", Year: " << year << endl;
}
};
int main() {
Car car1("Toyota Corolla", 2020);
car1.displayInfo();
// Using the copy constructor
Car car2 = car1; // Copy constructor is called
car2.displayInfo();
return 0;
}
Explanation:
Car object (car2) by copying the attributes (model and year) of car1.Car(const Car &other) is invoked when car2 is created as a copy of car1.In C++, if your class dynamically allocates memory (e.g., using new), you need to ensure that this memory is released when the object is destroyed. This is where the destructor comes in. For example:
#include <iostream>
using namespace std;
class Car {
private:
string* model;
int year;
public:
// Constructor
Car(string m, int y) {
model = new string(m); // Dynamically allocate memory for model
year = y;
cout << "Car created: " << *model << " (" << year << ")" << endl;
}
// Destructor
~Car() {
delete model; // Free the dynamically allocated memory
cout << "Car destroyed and memory released" << endl;
}
// Method to display car details
void displayInfo() {
cout << "Model: " << *model << ", Year: " << year << endl;
}
};
int main() {
Car car1("Honda Civic", 2022);
car1.displayInfo(); // Output car details
return 0; // Destructor automatically called here, memory released
}
Explanation:
model attribute is dynamically allocated using new in the constructor.delete keyword when the object is destroyed.Constructors:
Destructors:
Copy Constructors:
By using
Open this section to load past papers