Constructor
Constructor
Constructor
Definition
• Special member function with the same name as that of the class.
• Its purpose is to allocate appropriate memory to the objects of a class and
to initialize objects.
Example: class integer
{
int a,b;
public: integer( ) //constructor
{
a =b = 0;
}
};
Characteristics of a constructor
• They should be declared in the public section
• They are invoked automatically when the objects are created
• They can have default arguments.
• They can be overloaded
• They do not have return types, not even void. So they cannot return any
values.
• They cannot be inherited, but a derived class constructor can call the base
class constructor
• They cannot be virtual.
• An object with a constructor or destructor cannot be used as a member of
a union.
• They make implicit calls to the operators new and delete, when memory
allocation is required
Different types of constructors
Three types of constructors
1. Default constructor
• A constructor that accepts no arguments or parameters
• If user does not define a default constructor, the compiler supplies
it.
• It is invoked automatically , when the object is declared.
Example
integer obj; /*will automatically invoke the default constructor*/
[Note: here integer is the class name and obj is its object.]
2. Parameterized constructor
• Constructor that receives arguments.
• Can be used to initialise different objects of a class with different
values when they are created.
• Can be called implicitly or explicitly
Example:
/* implicit call */
integer obj1(10, 20);
integer obj2(30,40);
/* explicit call */
integer obj2 = integer(30, 40);
3. Copy constructor
• It accepts a reference to its own class as a parameter.
Example
class integer
{….
……
public: integer(integer &i)
};
int main( )
{
string name1(“Louis”);
…
….
}
Format for defining a constructor in a derived class
The header line of constructor definition consists of two parts separated
by a colon.
The first part includes the declaration of all arguments, passed to the
derived constructor.
The second part lists the function calls to the base constructor
Derived constructor( arglist1,arglist2,…..arglistN, arglistD) :
base1(arglist1), base2(arglist2),…baseN(arglistN)
{
Body of derived constructor
}
Example
Class alpha
{
int x;
public: alpha(int i ){ x = i; }
};
Class beta
{
float y;
public: beta( int j){ y = j; }
}
class gamma: public alpha, public beta
{
int m, n;
public: gamma( int a, float b, int c, int d: alpha(a), beta(b)
{
m = c;
n = d;
}
}
Destructor
Definition
• It is a special member function with the same name as that of the
class name, preceded by a tilde ( ̴ ) eg: ̴ integer( ) { }
• It is used to destroy objects created by the constructor
• Destructor is automatically executed by the compiler when the
object goes out of scope or upon exit from a program or a block.
• It is also invoked when delete operator is used to free memory
allocated with class pointer.
• A class can have only one destructor.
Characteristics of a destructor
• Destructor does not have a return type, not even void
• Destructor does not receive any arguments
• They do not have default values nor can be overloaded
• Destructors cannot be inherited, but the derived class can call the
destructor of the base class
• Destructors can be virtual, but constructors cannot
• Compiler can define a destructor if not explicitly defined.
• Destructor can make implicit calls to operator delete, if memory
de-allocation is needed for an object
• An object with a constructor or destructor cannot be used as a
member of a union.