WEB DEVELOPER SITE
PYTHONPHPJAVA
 

C++ Constructor and Destructor



class constructor

constructor of a class is a special member function of a class that is executed each time a new object of the class is created.

The name of the

constructor is exactly the same as the name of the class, and no type is returned, nor void is returned. Constructors can be used to set initial values for certain member variables.

The following example helps to better understand the concept of constructors:

example

#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); // This is the constructor private: double length; }; // Member function definitions, including constructors Line::Line(void) { cout << "Object is being created" << endl; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // Main function of the program int main( ) { Line line; // Set length line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }

When the above code is compiled and executed, it produces the following results:

Object is being created
Length of line : 6

Constructor with parameters

The default constructor takes no arguments, but the constructor can take arguments if needed. This will give the object an initial value when the object is created, as shown in the following example:

example

#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(double len); // This is the constructor private: double length; }; // Member function definitions, including constructors Line::Line( double len) { cout << "Object is being created, length = " << len << endl; length = len; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // Main function of the program int main( ) { Line line(10.0); // Get the length of the default settings cout << "Length of line : " << line.getLength() <<endl; // Set again length line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }

When the above code is compiled and executed, it produces the following results:

Object is being created, length = 10
Length of line : 10
Length of line : 6

Use the initialization list to initialize the field

Use the initialization list to initialize the field:

Line::Line( double len): length(len) { cout << "Object is being created, length = " << len << endl; }

The above syntax is equivalent to the following syntax:

Line::Line( double len) { length = len; cout << "Object is being created, length = " << len << endl; }

Assume that there is a class C with multiple fields X, Y, Z, etc. that need to be initialized. Similarly, you can use the above syntax, just separate the different fields with commas, as shown below: /p>

C::C( double a, double b, double c): X(a), Y(b), Z(c) { .... }

class destructor

The destructor of a class is a special member function of a class that is executed each time the created object is deleted.

The name of the destructor is exactly the same as the name of the class, except that it is preceded by a tilde (~) prefix, which does not return any value or any arguments. The destructor helps to free resources before jumping out of the program (such as closing files, freeing memory, etc.).

The following example helps to better understand the concept of a constructor:

example

#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); // This is the constructor declaration ~Line(); // This is the destructor declaration private: double length; }; // Member function definitions, including constructors Line::Line(void) { cout << "Object is being created" << endl; } Line::~Line(void) { cout << "Object is being deleted" << endl; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // Main function of the program int main( ) { Line line; // Set length line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }

When the above code is compiled and executed, it produces the following results:

Object is being created
Length of line : 6
Object is being deleted