In C++, constructors and destructors are special member functions that are used to create and destroy objects of a class, respectively. In this blog, we will explore constructors and destructors in more detail.
Constructors A constructor is a special member function that is called when an object of a class is created. The purpose of a constructor is to initialize the data members of the object. Constructors have the same name as the class and no return type, not even void.
In C++, there are two types of constructors: default constructors and parameterized constructors. A default constructor is a constructor that takes no arguments, while a parameterized constructor takes one or more arguments.
Here is an example of a default constructor for the ‘Person’ class:
csharpCopy codeclass Person {
private:
string name;
int age;
public:
Person() {
name = "";
age = 0;
}
void setName(string name) {
this->name = name;
}
string getName() {
return name;
}
void setAge(int age) {
this->age = age;
}
int getAge() {
return age;
}
};
In this example, we have defined a default constructor for the ‘Person’ class. The default constructor initializes the ‘name’ and ‘age’ data members to empty string and zero, respectively.
Here is an example of a parameterized constructor for the ‘Person’ class:
csharpCopy codeclass Person {
private:
string name;
int age;
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
void setName(string name) {
this->name = name;
}
string getName() {
return name;
}
void setAge(int age) {
this->age = age;
}
int getAge() {
return age;
}
};
In this example, we have defined a parameterized constructor for the ‘Person’ class. The parameterized constructor takes two arguments – a ‘name’ string and an ‘age’ integer – and initializes the ‘name’ and ‘age’ data members with these values.
Destructors A destructor is a special member function that is called when an object of a class is destroyed. The purpose of a destructor is to release any resources that the object has acquired during its lifetime. Destructors have the same name as the class with a tilde (~) symbol before it and no arguments or return types.
Here is an example of a destructor for the ‘Person’ class:
csharpCopy codeclass Person {
private:
string name;
int age;
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
~Person() {
cout << name << " has been destroyed." << endl;
}
void setName(string name) {
this->name = name;
}
string getName() {
return name;
}
void setAge(int age) {
this->age = age;
}
int getAge() {
return age;
}
};
In this example, we have defined a destructor for the ‘Person’ class. The destructor simply prints a message indicating that the object has been destroyed.
Constructors and Destructors in Action Here is an example of how constructors and destructors can be used in C++:
cCopy codeint main() {
Person p1; // Calls the default constructor
p1.setName("John");
p1.setAge(30);
cout << p1.getName() << " is " << p1.getAge() << " years