what is constructor?
Constructor is a function that is called by your program when object is created and it has following properties :- It has same name as that of class
- It has no return type (not even void)
what is destructor?
Destructor is function that is called by your program when object is destroyed and it has following properties :
- It has same name as that of class
- it has no return type
but then how would these two functions be distinguished? in destructor's name there is an extra tilde(~) symbol.
e.g.
class Dummy { public : Dummy() { cout<<"inside the constructor"<<endl; } ~Dummy() { cout<<"inside the destructor"<<endl; } };
P.S. - since I am a c++ programmer, so you will see all examples in C++.
But why was it introduced, how does it ease programmer's life?
To answer these questions, let us see how to create a student struct in C when OOPs was not there (note that when I say C, it means that I am talking about code that C compiler can compile)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | struct student { int roll_number; char *name; char *address; }; int main() { struct student S1,S2; S1.roll_number = 1; S1.name = (char*)malloc(7); //6 for abcdef + 1 for termination character strcpy(S1.name,"abcdef"); // similar code for address S2.roll_number = 2; S2.name = (char*)malloc(5); strcpy(S2.name,"zyxw"); //! do the other stuff like printing etc //! now we are done with student objects. //! let us clean them free(S1.name); free(S1.address); free(S2.name); free(S2.address); //! name and address has to be cleared because I created them with malloc //! and hence it is my responsibility to free the memory. //! for roll number, i didn't use malloc //! so it will be automatically deleted //! Ahh finally I am done } |
Now let us try to do same job with some object oriented programming language like C++ that supports constructor and destructor :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class student { int roll_number; char *name; char *address; public : student(int rno, char *n, char *a) { roll_number = rno; name = new char[sizeof(char)*strlen(n)]; strcpy(name,n); address = new char[sizeof(char)*strlen(a)]; strcpy(adress,a); } ~student() { delete [] name; delete [] adress; } }; int main() { student S1(1,"abcdef","some valid address1"); student S2(2,"zyxw","some valid address2"); } |
When line number 22 and 23 is hit, constructor of student class is called which automatically takes care of creating data members of object.
Similarly, when object goes out of scope, which is at the end of main function here, it is automatically deleted which in turn calls its destructor and in the destructor you have already taken care of clearing memory that you allocated using malloc/new.
Isn't it very simple? You just write your constructor and destructor carefully. Rest, everything will be taken care by compiler itself.
Now one may argue that it can be achieved using some C functions similar to constructor and destructor e.g. my_constructor and my_destructor.
It can be done. infact, people work only this way if they have to write code in C but it has its disadvantages. e.g.
User still have to call these functions explicitly. For construction it may still be fine. Like in above example, function will be called at #21 and #22 but one may forget to call destuctor function which in case of C++ compiler, it is automatically called.
No comments:
Post a Comment