In Object-Oriented Programming (OOP), constructors and destructors are special member functions used for managing the lifecycle of objects. They play a crucial role in object initialization and cleanup.
A constructor is a special member function that is automatically called when an object of a class is created. The main purpose of a constructor is to initialize the object's data members to a valid state. Constructors do not return any value, not even void.
Automatic Invocation:
No Return Type:
void.Same Name as Class:
Overloading:
Default Constructor:
Parameterized Constructor:
Copy Constructor:
#include <iostream>
using namespace std;
class Car {
private:
string brand;
int year;
public:
// Default Constructor
Car() {
brand = "Unknown";
year = 0;
}
// Parameterized Constructor
Car(string b, int y) {
brand = b;
year = y;
}
// Copy Constructor
Car(const Car &c) {
brand = c.brand;
year = c.year;
}
// Method to display details
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
// Creating an object using the default constructor
Car car1;
car1.displayDetails();
// Creating an object using the parameterized constructor
Car car2("Toyota", 2020);
car2.displayDetails();
// Creating an object using the copy constructor
Car car3 = car2; // Copy car2 to car3
car3.displayDetails();
return 0;
}
Car() constructor initializes brand to "Unknown" and year to 0.Car(string b, int y) constructor initializes the Car object with specific values for brand and year.Car(const Car &c) constructor creates a copy of an existing Car object (car2) and assigns its values to the new object (car3).A destructor is a special member function that is called automatically when an object goes out of scope or is explicitly deleted. The primary purpose of a destructor is to release resources (such as memory, file handles, or network connections) that were acquired during the lifetime of the object.
Automatic Invocation:
delete is called (for dynamically allocated objects).No Arguments:
Same Name as Class (with ~ prefix):
Only One Destructor per Class:
Memory Cleanup:
#include <iostream>
using namespace std;
class Car {
private:
string brand;
int year;
public:
// Constructor to initialize the object
Car(string b, int y) {
brand = b;
year = y;
cout << "Car " << brand << " created." << endl;
}
// Destructor to clean up resources
~Car() {
cout << "Car " << brand << " destroyed." << endl;
}
// Method to display car details
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
// Creating an object of Car
Car car1("Toyota", 2020);
car1.displayDetails();
// Destructor is automatically called when car1 goes out of scope at the end of main
return 0;
}
Car object car1 goes out of scope at the end of the main() function, the destructor ~Car() is automatically called to clean up any resources (in this case, just printing a message).Destructors are particularly important when dealing with dynamic memory (memory allocated using new in C++). If objects dynamically allocate memory or other resources, the destructor ensures that these resources are properly freed when the object is destroyed.
#include <iostream>
using namespace std;
class Car {
private:
string *brand;
int year;
public:
// Constructor to allocate memory for the brand
Car(string b, int y) {
brand = new string(b); // Dynamically allocate memory
year = y;
cout << "Car " << *brand << " created." << endl;
}
// Destructor to release dynamically allocated memory
~Car() {
delete brand; // Free dynamically allocated memory
cout << "Car memory for " << *brand << " destroyed." << endl;
}
// Method to display car details
void displayDetails() {
cout << "Brand: " << *brand << ", Year: " << year << endl;
}
};
int main() {
Car car1("Toyota", 2020);
car1.displayDetails();
// Destructor is automatically called when car1 goes out of scope
return 0;
}
brand is a pointer to a string that is dynamically allocated using new.~Car() uses delete to free the memory allocated for brand when the object is destroyed.brand would not be released, leading to a memory leak.Together, constructors and destructors allow for efficient and safe memory and resource management in object-oriented systems.
Open this section to load past papers