Constructors and Destructors
A class constructor can be defined as a special member function of a class that is executed whenever we create new objects of that class. A constructor shares the same name as that of the class.
A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors are very useful for setting initial values for a certain set of member variables.
We will now look at the following example to understand the concept of constructor in a detailed manner.
#include <iostream> using namespace std; class Sample { public: void setLen( float l ); float getLen( void ); Sample(); // This is the constructor private: float len; }; // Member functions definitions including constructor Sample::Sample(void) { cout << "Object created" << endl; } void Sample::setLen( double l ) { len = l; } float Sample::getLen( void ) { return len; } // Main function for the program int main() { Sample sample; // set sample len sample.setLen(8.0); cout << "Length of sample : " << sample.getLen() <<endl; return 0; }
Output:
Object is being created
Length of line : 8
Parameterized Constructor:
A default constructor, usually does not have any parameter. However, if needede, a constructor can be assigned parameters. This feature allows us to assign an initial value to an object when it is being created.
Example:
#include <iostream> using namespace std; class Sample { public: void setLen( float l ); float getLen( void ); Sample(float l); // constructor with parameter l private: float len; }; // Member functions are defined here Sample::Sample( float l) { cout << "Object created, length = " << l << endl; len = l; } void Sample::setLen( float l ) { len = l; } float Sample::getLen( void ) { return len; } // Main function int main() { Sample sample(20.0); // Initial value of len cout << "Length of sample : " << sample.getLen() <<endl; // set line length again sample.setLen(8.0); cout << "Length of sample : " << sample.getLen() <<endl; return 0; }
Output:
Length of line : 20
Length of line : 8
The Class Destructor:
A destructor can be defined as a special member function of a class which is implemented whenever one of the objects pertaining to it’s class slips out of scope or whenever the delete expression is applied to a pointer to the given object.
A destructor also has the same name as that of the class, which is preceded by a tilde (~) and it neither returns a value nor does it take any parameters. Destructors are useful for releasing resources before closing the program.It helps in freeing up memory spaces that are not needed anymore. Example-
#include <iostream> using namespace std; class Sample { public: void setLen( float l ); float getLen( void ); Sample(); // This is the constructor declaration ~Sample(); // This is the destructor: declaration private: float len; }; // Member functions definitions Sample::Sample(void) { cout << "Object created" << endl; } Sample::~Sample(void) { cout << "Object deleted" << endl; } void Sample::setLen( float l ) { len = l; } float Sample::getLen( void ) { return len; } // Main function int main() { Sample sample; sample.setLen(8.0); cout << "Length of sample : " << sample.getLen() <<endl; return 0; }
Output:
Object created
Length of sample : 8
Object deleted
Login/Signup to comment