C++ Class Constructors & Destructors
Class Constructor
A class constructor is a special member function of a class that is executed whenever a new object of the class is created.
The constructor's name is identical to the class name and it does not return any type, nor does it return void. 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 the constructor
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 result:
Object is being created
Length of line : 6
Constructor with Parameters
The default constructor does not have any parameters, but if needed, a constructor can also have parameters. This allows initial values to be assigned to the object at the time of creation, 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 the constructor
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 default set length
cout << "Length of line : " << line.getLength() <<endl;
// Set length again
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 result:
Object is being created, length = 10
Length of line : 10
Length of line : 6
Initializing Fields Using Initialization List
Fields can be initialized using an initialization list:
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;
}
Suppose there is a class C with multiple fields X, Y, Z, etc., that need to be initialized. Similarly, you can use the above syntax, separating different fields with commas, as shown below:
C::C( double a, double b, double c): X(a), Y(b), Z(c)
{
// Constructor body
}
This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation. Chinese: .... }
Destructor of a Class
A destructor is a special member function of a class that is executed whenever an object of it's class is destroyed.
The name of the destructor is the same as the class name preceded by a tilde (~). It cannot return a value nor can it take any parameters. Destructors help to free up resources before exiting the program, such as closing files and freeing memory.
The following example helps to better understand the concept of a destructor:
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 the constructor
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 result:
Object is being created
Length of line : 6
Object is being deleted