Thursday, May 13, 2021
Home Classes in C++ constructors and destructors in C++

constructors and destructors in C++

We can use a constructor as a function and can pass arguments to it, but it must have no return type. Destructors are also member function of the class which Performs termination housekeeping before the system reclaims the object’s memory.  It is simply the Complement of the constructor. There can only be one destructor inside a class with no return type and receives no parameters and used with a sign “ ~ ” before its name.
We will get it clear from the below example:

  1. //constructors and destructors in c++ example
  2. //constructors and destructors, constructors with different arguments
  3. #include<iostream>
  4. #include<string>
  5. using namespace std;
  6. class point
  7. {
  8. private:
  9.     int x;
  10.     int y;
  11. public:
  12.     point();
  13.     point(int);
  14.     point(int,int);
  15.     ~point()
  16.     {
  17.     cout<<“THE DESTRUCTOR IS CALLED”<<endl;
  18.     };
  19.     void print();
  20. };
  21.  
  22. point::point()
  23. {
  24.     x= 0;
  25.     y = 0;
  26.     cout<<“no argument “<<“x = “<<x<<” and y = “<<y<<endl;
  27. }
  28.  
  29. point::point(int a)
  30. {
  31.     x = a;
  32.     y = a;
  33.     cout<<“with one argument “<<“x = “<<x<<” and y = “<<y<<endl;
  34. }
  35.  
  36. point::point(int a, int b)
  37. {
  38.     x= a;
  39.     y = b;
  40.     cout<<“with two arguments “<<“x = “<<x<<” and y = “<<y<<endl;
  41. }
  42.  
  43. int main()
  44. {
  45.     point p;
  46.     point p1(1);
  47.     point p2(6,5);
  48.  
  49.     return 0;
  50. }

Now we defined a class point, you can see that we used our constructors and destructors in it, both constructors and destructors are defined in a manner as described before, we described constructors in three different manners, one with no argument, second with one argument and third with two arguments and initialized the values of the private members. Similarly we called a destructor with a “ ~ ” sign before it and its description. In main() we called all our constructors in three different ways. Now after execution all constructors and destructors is automatically called, means that destructor is called three times, So for every constructor there is a destructor, no matter whatever that is. And that’s all about constructors and destructors.

 

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....