Professional Documents
Culture Documents
Chapter 6
Types of constructors
Depending on how the member data of objects are provided with values while
they are declared. C++ provides 4 types of constructors.
1. Default constructors.
2. Parameterized constructors or argument constructors.
3. Overloaded constructors.
4. Copy constructors.
Suppose xyz is a class, the default constructor in the class takes the
following form:
xyz( )
{
statements;
}
The statements within the body of the function assign the values to the data
members of the class.
2. Parameterized constructor:
A constructor with one or more arguments is called Parameterized constructor.
Since constructor is a function similar to other functions in C++, it is possible to pass
arguments to the constructors from the main function.
3. Overloaded constructors:
A class may have zero or more constructors. If Two or more constructors
exists in the same class, they differ in their argument list such constructors are called
overloaded constructors.
DESCTRUCTORS
A destructor is a special type of member function called automatically when the
object scope is ended.
A destructor has the same name as the constructor (same as the class name) but
prefixed by tilde (~) operator. The role of destructor is to deallocate the memory block
allocated by the constructor.
Characteristics:
1. Destructors do not have return type.
2. It should be a public member.
3. Destructors do not have any argument.
4. There should be only one destructor in each class.
5. Destructors cannot be overloaded.
Review Questions