4-Constructors and Destructors
4-Constructors and Destructors
U College,
Chickballpur
public:
example () //constructor inside the class definition
{
a = 0;
}
};
Default Constructor
A constructor which does not accept any arguments is called a zero argument
constructoror default constructor.
Default constructor simply allocates memory to data members of objects.
Features of Default Constructor
For every object created, this constructor is automatically called.
All objects of a class are initialized to same set of values by the default constructor.
If different objects are to be initialized with different values, it cannot be done using
default constructor.
Example:
Syntax: book()
classname::classname()//constructor without arguments {
{
}
}
Program to show initialization of an object using default constructor
#include<iostream.h>
#include<iomanip.h>
class decon
{
private:
int x, y;
public:
decon ( )
{
x=10; y=20;
}
void display()
{
cout<<"x = "<<x<<"y = "<<y<<endl;
}
}; x = 10 y = 20
x = 10 y = 20
void main()
{
decon D1,D2;
D1.display();
D2.display();
}
Disadvantages
When many objects of the same class are created, all objects are initialized to
same set of values by default constructor.
It is not possible to initialize different objects with different initial values.
Parameterized Constructors
A constructor that takes one or more arguments is called parameterized constructor.
It is possible to initialize different objects with different values.
Parameterized constructors are also invoked automatically whenever objects with
arguments are created.
The parameters are used to initialize the data members of the object.
Features:
Parameterized constructors can be overloaded.
For an object created with one argument, constructor with only one argument is invoked
and executed.
void main()
{
num N1=num(10,20); //Explicit call of object1
num N2=num(40,50); //Explicit call of object2
N1.display();
N2.display();
}
Implicit call:An Implicit call means the declaration of the object is followed by argument list
enclosed in parentheses.
Example: num obj1(10,20);
num obj2(40,50);
To illustrate the use of parameterized constructor through implicit call.
#include<iostream.h>
class num
{
private:
int a,b;
public:
num(int p, int q)
{
a = p;
b = q;
}
void display()
{
cout<<"a = "<<a<<" and b = "<<b<<endl;
}
};
void main()
{
num N1(10,20); //Implicit call of object1
num N2(40,50); //Implicit call of object1
N1.display();
N2.display();
}
Initialization using assignment operator or constructor with one parameter
This method is used for the constructor with exactly one argument.
In this method, declaration is followed by assignment operator and value to be initialized.
This method is applicable only to the constructors that have exactly one
parameter.
Example: num obj1=100;
num obj2=400;
#include<iostream.h>
class num
{
private:
int a;
public:
num(int p)
{
a = p;
}
void display()
{
cout<<"a = "<<a<<endl;
}
};
void main()
{
num N1=100;
num N2=400;
N1.display();
N2.display(); }
Copy constructor It is a parameterized constructor.
One object can be copied to another object.
Copy constructors are used when:
To initialize an object with the values of existing object.
To return an object as return-type.
To state objects as pass-by- value parameters of a function.
Copy constructor can accept a single argument of reference to same class type. The
argument must be passed as a constant reference type.
Syntax: classname:: classname(classname &ptr)
Example: x::x(x &ptr)
Here, x is a class name and ptr is a pointer to a class object x.
If there is more than one argument present in the copy constructor, it must
contain default arguments.
Note: 1.Copy constructor is not invoked explicitly.
2. Copy constructor is invoked automatically when a new object is created and
equated to an already existing object in the declaration statement itself.
Example: x a1; //default constructor
x a2 = a1; //copy constructor
a1.display ();
Copy constructor creates a new object a2 using existing object a1.
3. When a new object is declared and existing object is passed as a parameterto it,
then also copy constructor is invoked.
Example: x a1(100,200); //parameterized constructor
x a2(a1); //copy constructor is invoked for object a2
with a1 as parameter
4. When an object is passed to a function using pass-by-value, copy constructor is
automatically called.
Example: void test(x b)
{
—————
—————
}
void main()
{
x a;
a.test(a); //copy constructor is invoked
}
5. Copy constructor is invoked when an object returns a value.
Constructor Overloading
Constructors are used to initialize the data members of a class.
Destructors are special member functions of the class required to free the memory of
the object whenever it goes out of scope.
A Destructor has same name that of the class name preceded by a tilde symbol (~).
Destructor is a special member function that de-allocates the memory allocated to the
object.
A destructor will be called automatically when an object to be destroyed.
Destroying an object means, de-allocating all the resources such as memory that was
allocated for the object by the constructor.
Destructors do not have a return value. Destructor can never return a value.
They take no arguments. Therefore, destructors cannot be overloaded.
Syntax: classclassname
{
private:
//datavariables
public:
classname(); //constructor
~classname(); //destructor
};
#include<iostream.h>
#include<conio.h>
class number
{
private:
int a;
public:
number(); //Constructor
void display();
~number(); //Destructor
};
number::number()
{
cout<<“Constructor: ”<<endl;
a=10;
}
number::~number()
{
cout<<“Destructor ”<<endl;
}
void number::display()
{
cout<<“Value of a = ”<<a<<endl;
}
int main()
{
number N;
N.display();
return 0;
}