C++ Class Constructors and Destructors

C++ class constructors and destructors are members of a class.

C++ Class Constructors

Constructors are special class functions that execute with the creation of class object. The constructor initializes the object of the class and allocates memory to the object. Class constructor has no return type, not even void. It has same name as class. Constructors may or may not have arguments. The syntax for defining constructor is:

Default Constructors

That constructors that takes no arguments are default constructors. Default constructor is important for object initialization. The cases where programmer does not create the default constructor, compiler creates it on its own. For example, the code below illustrates how to use constructors. Here the class name is printText and constructor’s name is also printText. The constructor printText prints a simple text. The code executes the constructor as soon as it finds the object of class.

#include <iostream>
using namespace std;
class printText {   
  public:  
    printText() {     // Constructor
      cout << "Hello, how are you?";
    }
};
int main() {
  printText text1;
  return 0;
}

The output of above code after compilation is:

There are two ways of defining a constructor. Programmer can either define a constructor inside the class definition or outside it. In the above example, constructor definition lies within the class definition.

The programmer uses resolution operator (::) for defining the constructor outside the class definition. The syntax for defining a constructor outside the class definition is writing class name followed by resolution operator followed by constructor name.

For example, in the following code the constructor printText is declared within the class, but its definition is present outside the class.

#include <iostream>
using namespace std;
class printText {   
  public:  
    printText();
};
printText::printText() {     // Constructor
      cout << "Hello, how are you?";
    }
int main() {
  printText text1;
  return 0;
}

The output of the above code after compilation is:

Parameterized Constructors

Constructors can have parameters too. The constructors having parameters are parameterized constructors. For example, the code below shows a constructor Book having three arguments. It takes book title, book author and book pages as constructor’s parameters.

#include <iostream>
#include <string>
using namespace std;
class Book {        
  public:          
    string title;  
    string author;  
    int pages;      
    Book(string t, string a, int p);
};
Book::Book(string t, string a, int p) {
  title = t;
  author = a;
  pages = p;
}
int main() {
  Book book1("Harry Potter", "JK Rowling", 500);
  Book book2("Introduction to Programming with C++", "Diane Zak", 786);
  cout << book1.title << " written by " << book1.author << ", pages " << book1.pages << "\n";
  cout << book2.title << " written by " << book2.author << ", pages " << book2.pages << "\n";
  return 0;
}

The output of the above code after compilation is:

C++ Class Destructors

C++ class destructors are also special functions of a class like constructors. As the name suggests destructors destroy the object of a class and clear the memory allocated to it. Destructor executes when the object of class goes out of scope. Destructors have same name as class name but destructor name is followed by tilde sign (~). Any class can have only one destructor. The syntax for defining a destructor is:

Destructors do not have any parameters or return type. So, they do not provide overloading functionality. For examples, the code below illustrates the concept of destructors. It is automatically called when programmer creates an object. Note that the sequence of constructors and destructors is that first constructor is called and then destructor is called.

#include <iostream>
using namespace std;
class printText {   
  public:  
    printText() {     // Constructor
      cout << "This is a constructor" << endl;
    }
    ~printText() {    // Decstructor
      cout << "This is a destructor";
    }
};
int main() {
  printText text1;
  return 0;
}

The output of the above code after compilation is:

In the above example, all three class, constructor and destructor have the same name. The destructor printText deletes the object that the class creates, after the execution of program. Destructors are useful for freeing space and memory of resources that are no longer needed.

Like constructors, destructor can also be defined out of the class definition. The syntax for defining a destructor outside class definition is:

Note that when constructor or destructor is defined outside the class, it should always be first declared inside the definition of class. For example, the code below shows how to define a destructor outside the class definition.

#include <iostream>
using namespace std;
class printText {   
  public:  
    printText() {     // Constructor
      cout << "This is a constructor" << endl;
    }
    ~printText();
};
printText::~printText() {    // Decstructor
      cout << "This is a destructor";
    }
int main() {
  printText text1;
  return 0;
}

The output of the above code after compilation is: