Hi,
I want to know the concept of Constructors and Destructors. Anyone can explain me this in simple words?
Hi,
I want to know the concept of Constructors and Destructors. Anyone can explain me this in simple words?
Constructor and Destructor functions are Member Functions of a class having some special property.
Constructor function gets invoked when an object of a class is constructed (declared) and destructor function gets invoked when the object is destructed (goes out of scope).
Use of Constructor and Destructor function of a class
- Constructor function is used to initialize member variables to pre-defined values as soon as an object of a class is declared.
- Constructor function having parameters is used to initialize the data members to the values passed values, upon declaration.
- Generally, the destructor function is needed only when constructor has allocated dynamic memory.
Defining Constructor and Destructor functions
The example below illustrates how constructor and destructor functions are defined:
A few points to note:Code:class myclass { private: int number; public: myclass()//constructor { number=10; } ~myclass()//destructor { //nothing needed } };
- Both of the functions have the same name as that of the class, destructor function having (~) before its name.
- Both constructor and destructor functions should not be preceded by any data type (not even void).
- These functions do not (and cannot) return any values.
- We can have only the constructor function in a class without destructor function or vice-versa.
- Constructor function can take arguments but destructors cannot.
- Constructor function can be overloaded as usual functions.
Example 1: Using constructor function to initialize data members to pre-defined values
Example 2: Using constructor function to initialize data members to values passed as argumentsCode://Example Program in C++ #include<iostream.h> class myclass { private: int a; int b; public: myclass() { //here constructor function is used to //initialize data members to pre-def //values a=10; b=10; } int add(void) { return a+b; } }; void main(void) { myclass a; cout<<a.add(); }
Notice that there is no destructor function in both the examples, just because we don’t need them.Code://Example Program in C++ #include<iostream.h> class myclass { private: int a; int b; public: myclass(int i, int j) { a=i; b=j; } int add(void) { return a+b; } }; void main(void) { //notice how the object of the class //has been declared //it can be thought as // myclass a; // a=myclass(10,20) myclass a(10,20); cout<<a.add(); }
When does the destructor function gets invoked
The examples below illustrates when the destructor function gets invoked:
Example 1:
OUTPUT:Code://Example Program in C++ #include<iostream.h> class myclass { public: ~myclass() { cout<<"destructed\n"; } }; void main(void) { myclass obj; cout<<"inside main\n"; }
inside main
destructed
Press any key to continue
As I said in the other article, destructors get invoked when the object of a class goes out of scope. In this case, the object goes out of scope as the program terminates. So the destructor gets invoked just before the program’s termination.
Example 2:
OUTPUT:Code://Example Program in C++ #include<iostream.h> void myfunc(void); class myclass { public: ~myclass() { cout<<"destructed\n"; } }; void main(void) { cout<<"inside main\n"; myfunc(); cout<<"again inside main\n"; } void myfunc(void) { cout<<"inside myfunc\n"; myclass obj; cout<<"still inside myfunc\n"; }
inside main
inside myfunc
still inside myfunc
destructed
again inside main
Press any key to continue
In this case, destructor function is invoked just as the program’s execution returns from the function, but before executing any further instruction from where it was called (main).
Example 3: In the following example we are creating a dynamically allocated object of a class in the same way as we did with the variables.
OUTPUT:Code://Example Program in C++ #include<iostream.h> class myclass { public: ~myclass() { cout<<"destructed\n"; } }; void main(void) { myclass *obj; obj=new myclass; cout<<"inside main\n"; delete obj; cout<<"still inside main\n"; }
inside main
destructed
still inside main
Press any key to continue
Here the programmer is explicitly destroying the object, hence the destructor function is called.
Now that you know some details about the destructor function, let me give you a practical example of the use of destructor function:
Good-Bye!Code://Example Program in C++ #include<iostream.h> class myclass { int *number; public: myclass(int num) { number=new int[num]; } ~myclass() { delete []number; } void input_num(int index, int num) { number[index]=num; } int output_num(int index) { return number[index]; } }; void main(void) { int size, num; cout<<"enter number of elments: "; cin>>size; myclass obj(size); for(int i=0;i<size;i++) { cout<<"enter element "<<i+1<<":"; cin>>num; obj.input_num(i,num); } cout<<"\nelements have the following values\n\n"; for(i=0;i<size;i++) { cout<<"element "<<i+1<<":"; cout<<obj.output_num(i); cout<<"\n"; } }
A constructor is a member function that has the same name as its class (and which has no return value). E.g., there are four constructors declared in Program. The purpose of a constructor is to initialize an object. In fact, C++ makes the following promise: If a class has a constructor, all objects of that class will be initialized.
Consider the following sequence of variable declarations:
Each variable declaration implicitly causes the appropriate constructor to be invoked. In particular, the equal sign in the declaration of d denotes initialization not assignment and, therefore, is accomplished by calling the constructor.Code:Complex c; // calls Complex () Complex d = 2.0; // calls Complex (double) Complex i(0, 1); // calls Complex (double, double) Complex j(i); // calls Complex (Complex const&)
details
Fore more details understanding go through this!
http://publib.boulder.ibm.com/infoce...c15cplr374.htm
Hope this helps you!
Bookmarks