Destructor in C++

A destructor is another type of special member function of a class. The name of the destructor for a class is the title(~) character followed by the class name. A class’s destructor is called when an object is destroyed. This occurs, for example, as an automatic object is destroyed when program execution leaves the scope in which that object was instantiated. The destructor itself does not actually destroy the object it performs termination housekeeping before the system reclaims the object’s memory so that memory may be reused to hold new objects.

A destructor receives no parameters and returns no value. A class may have only one destructor(destructor overloading is not allowed)

Errors comes when you do this:

It is a syntax error to attempt to pass arguments to a destructor, to specify a return type for a destructor(even void cannot be specified), to return values from a destructor or to overload a destructor.

When to Call Constructors and Destructors

Constructors and destructors are called implicitly by the compiler. The order in which these function calls occur depends on the order in which execution enters and leaves the scopes where the objects are instantiated. Generally, destructor calls are made in the reverse order of the corresponding constructor calls. The storage classes of objects can alter the order in which destructors are called.

Demonstrating the order in which constructors and destructors are called

 using namespace std; 
 class Employee 
 cout<<"Constructor Invoked"<<endl; 
 cout<<"Destructor Invoked"<<endl; 
 int main(void) 
 Employee e1; //creating an object of Employee 
 Employee e2; //creating an object of Employee 
 return 0; 


Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked

Using Set and Get Functions

A class’s private data members can be accessed only by member functions (and friends) of the class. A typical manipulation might be the adjustment of a customer’s bank balance(e.g. a private data member of a class BankAccount) by a member function computeInterest.

Classes often provide public member functions to allow clients of the class to set or get the values of private data members. These functions need not be called set and get specific, but they often are. More specifically, a member function that sets data member interestRate might be named setInterestRate, and a member function that gets the interestRate might be named getInterestRate.Get functions are also commonly called “query” functions.

It may seem that providing both set and get capabilities is essentially the same as making the data members public. This is yet another subtlety of C++ that makes the language so desirable for software engineering. If a data member is public, then the data member can be read or written at will by any function in the program. If a data member is private, a public get function would certainly seem to allow other functions to read the data at will. However, the get function could control the format in which the data is returned to the client. A public set function could and most likely would carefully scrutinize any attempt to modify the value of the data member. This would ensure that the new value is appropriate for that data item

Translate »