Constructors and Destructors
Constructors: It is a special member function. This is used to give initial values to member variables. General form is
Constructor name (parameters)
{
statements;
}
Where,
constructor name – same as name of the class.
Parameters – it can be void or can have a list of arguments.
Rules:
There are three types of constructors. They are
a)constructors without parameters:
If a constructor has no parameters or arguments then it is called constructor without parameters.
b)constructor with parameters:
If a constructor has arguments then it is called constructor with parameters.
The value of the parameters should be given along the object declaration.
General form:
Class_name object = constructor_name (argument_value_list);
Class_name – name of the defined class.
Constructor_name - name of the defined class.
It can be used to initialize the member variables. This is called explicit call.
c) Multiple constructors:
If a class has more than one constructor then it is said to be Multiple constructors. This technique is called overloading constructors.
Constructors with default argument:
If we give initial values to the member variables inside the argument list of constructor then it is called constructor with default arguments.
General form:
a)Declaration:
Constructor_name (argument_type argument_variable = value1, argument_type argument variable = value 2, ……..);
b)Definition:
Constructor_name (argument_type argument_variable = value1, argument_type argument variable = value 2, ……..);
{
member_variable1 = argument_variable1;
member_variable2 = argument_variable2;
- - - - - - - - - - -- - -- -- - - -- - - -- - - - - -
- - - - -- - -- - -- - -- -- - - - -- - - - -- -- -- --
}
At the time of object declaration, depending upon the value, the constructor automatically assigns the values to member variables. If any value is missing, this will be taken from the argument list values in the declaration or definition of constructor.
Dynamic initialization of objects:
Dynamic initialization of objects means giving initial values to the member variables during run time. With the help of this facility, we can give different type of values to the member variables depending upon the situation.
Rules:
Copy constructors:
General form:
Constructor_name (class_name & object_name)
{
----- - - -- - -- -- - -- - - -- - ----- - --
- - -- - - -- - -- - -- - - - - - - - - - - - -
- - - - -- - - - - - - - - - - - - - - - - - - --
}
where
&object_name – valid C++ object with values
Copy constructor calling:
General form:
Class_name new_object_name = old_object_name;
Where,
New_object_name – object to be initialized
Old_object _name - object already having classes.
When this is executed the values in the old object name are copied to new object name.
Rules:
Dynamic constructors:
If the memory space for the constructor is allocated during the constructor calling, then it is called dynamic constructor. This is done with the help of the operator new. Depending on the size of the values in the variable the memory occupation of the object varies.
Destructors:
Destructor is a member function used to deallocate the memory space allocated by the constructor. General form:
~ destructor name( )
{
- - - - - - - - - - -
- - - -- - - - - -- - -
- -- - - -- - - - - - --
}
Where
~ (tilde) – destructor operator
destructor name – name of class
The compiler automatically deallocates the memory space when the program ends.
Rules:
| Name* : |
|||||
| Email* : |
|||||
| Country* : |
|||||
| Phone* : |
|||||
| Subject* : |
|||||
| Upload Homework : Upload another homework (upto 5 uploads max.)
|
|||||
| Due Date |
Time |
AM/PM |
Timezone |
||
| Instructions |
|||||
|
|||||