Constructors and Destructors
Constructors and destructors in C++
Classes provide a special way of initializing objects – through the use of constructors. Similarly certain commands can be exeuted before any object is deleted- this is done via destructors.
a few notes on constructors/ destructors
- constructors and destructors do not have return types (not even void) nor can they return values.
- references and pointers cannot be used on constructors and destructors because their addresses cannot be taken.
- constructors and destructors cannot be declared static, const
- constructors and destructors obey the same access rules as member functions.
- Like other member functions, constructors and destructors are declared within a class declaration. They can be defined inline or external to the class declaration.
- Derived classes do not inherit constructors or destructors from their base classes, but they do call the constructor and destructor of base classes.
A constructor is a member function of the class that is called automatically whenever an object is created.
- constructors cannot be declared with the keyword virtual.
- a constructor does not allocate memory for the class object. It simply initializes the members of that object
- constructor name is same as that of the class it belongs to
Just the way we have function overloading , we can overload constructors as well.
The following example demonstrates the use of constructor in different forms
in the above program
- we have declared four constructors
o with no argument
o with one argument
o two overloaded constructors with two arguments each
- for invoking the required constructors
two formats can be used
- a=STUDENT(parameter list) or simply a(parameter list)
- the use of destructors has also been shown. The output also shows that objects are deleted in the reverse order in which they are created
You can call a constructor only while creating a object and not afterwards
STUDENT a ;
a(34,5); //this will result in an error
If you DO NOT DECLARE your own constructor/destructor the compiler automatically creates a default one . This default constructor initializes the members to some garbage value.
In fact these are not the only members that the compiler creates automatically. The other two are the copy constructor and copy assignment operator.
This is one elusive aspect of the constructors.
This constructor is automatically invoked whenever a copy of a object is required to be created
consider the following two statements
The first statement looks familiar. It copies obj2 to obj1 member wise. What is unknown here is that operator overloading is being used here to overload the assignment operator. Further details on this are being omitted for clarity and will be dealt later on.
The second statement also performs the same task, it copies obj2 into obj1 member-wise. This is the copy constructor.
The declaration of copy constructor is as follows
The argument to copy constructor is always passed by reference. If this is not the case then a “out of memory “ error occurs. This is because if we had used call by value then for creating copies of the argument , again copy constructor would be called, and this would recursively continue till we run out of memory.
The copy constructor is always invoked for creating the copies of the argument
whenever we pass a class object as argument to any function
It may seem that the copy constructor is not required. But the following program illustrates the use of copy constructor.
this program shows the copy constructor in action.
When we call the function display() , since it is a call be value , a copy of the argument object is required which invokes the copy constructor.
In the display_ref() , call by reference is used , so no need for copy constructor
- You don't call them explicitly (they are called automatically for you), and there's only one destructor for each object.
- The name of the destructor is the name of the class, preceeded by a tilde (~)
- Destructors can be declared with the keyword virtual.
- A destructor does not de-allocate memory for the class object . It simply executes the commands in its definition just before its associated object is about to be deleted.
If some object has been created using new() operator then it will be deleted only when delete() is used and only then will its destructor be called.