This topic is about D Programming – Constructor & destructor.
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
A constructor has exactly the same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.
The following example explains the concept of constructor −
import std.stdio; class Line { public: void setLength( double len ) { length = len; } double getLength() { return length; } this() { writeln("Object is being created"); } private: double length; } void main( ) { Line line = new Line(); // set line length line.setLength(6.0); writeln("Length of line : " , line.getLength()); }
When the above code is compiled and executed, it produces the following result −
Object is being created Length of line : 6
Parameterized Constructor
A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation as shown in the following example −
Example
import std.stdio; class Line { public: void setLength( double len ) { length = len; } double getLength() { return length; } this( double len) { writeln("Object is being created, length = " , len ); length = len; } private: double length; } // Main function for the program void main( ) { Line line = new Line(10.0); // get initially set length. writeln("Length of line : ",line.getLength()); // set line length again line.setLength(6.0); writeln("Length of line : ", line.getLength()); }
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
The Class Destructor
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.
A destructor has exactly the same name as the class prefixed with a tilde (~). It can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.
The following example explains the concept of destructor −
import std.stdio; class Line { public: this() { writeln("Object is being created"); } ~this() { writeln("Object is being deleted"); } void setLength( double len ) { length = len; } double getLength() { return length; } private: double length; } // Main function for the program void main( ) { Line line = new Line(); // set line length line.setLength(6.0); writeln("Length of line : ", line.getLength()); }
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
In this topic we learned about D Programming – Constructor & destructor. To learn more, Click Here.
Pingback: D Programming - Classes & Objects - Adglob Infosystem Pvt Ltd