Constructors
Constructors
Constructors
Destructors in C++
1
Constructors
• A special member function whose task is to initialize the objects of its class.
• It has the same name as the class name.
• It is invoked whenever an object of its associated class is created.
• A constructor is declared and defined as follows:
class integer
{
int m, n;
public:
integer(void); //constructor declared
…..
…..
};
2
integer :: integer(void) // constructor defined
{
m=0; n=0’
}
• When a class contains a constructor, it is guaranteed that an object
created by the class will be initialized automatically. For example:
integer int1; //object int1 created
• The above statement not only creates the object int1 of type integer but
also initializes its data members m and n to zero.
• A constructor that accepts no parameters is called the default
constructor.
• If no such constructor is defined, then the compiler supplies a default
constructor.
3
Special characteristics of
constructors
• They should be declared in the public section.
• They are invoked automatically when the objects are created.
• They do not have return types, not even void and therefore, they
cannot return values.
• They cannot be inherited, though a derived class can call the base
class constructor.
• They can have default arguments.
4
Parameterized Constructors
• The constructors that can take arguments are called parameterized
constructors.
• When a constructor has been parameterized, the object declaration
statement such as
integer int1;
• May not work. We must pass the initial values as arguments to the
constructor function when an object is declared.
• This can be done in two ways:
• By calling the constructor explicitly
• By calling the constructor implicitly
5
#include<iostream>
using namespace std;
class integer
{
int m, n;
public:
integer(int, int);
void display(void)
{
cout<< “m = ”<<m<<“\n”;
cout<< “n = ”<<n<<“\n”;
}
};
6
integer :: integer(int x, int y)
{
m=x; n=y;
}
int main()
{
integer int1(0, 100); //constructor called implicitly
integer int2 = integer(25, 75); //constructor called explicitly
cout<<“\nOBJECT1”<<“\n”;
int1.display();
cout<<“\nOBJECT2”<<“\n”;
int2.display();
return 0;
}
7
output:
OBJECT1
m=0
n=100
OBJECT2
m=25
n=75
8
Multiple constructors in a class
• A class can have more than one constructors. For example, a class can be defined as
follows:
class integer
{
int m, n;
public:
integer() //constructor 1
{ m=0; n=0;}
integer(int a, int b) //constructor 2
{ m = a; n = b; }
integer(integer &i) //constructor 3
{ m = i.m; n = i.n; }
};
9
• This declares three constructors for an integer object.
• The first constructor receives no arguments, the second constructor receives two
integer arguments and the third receives one integer object as an argument.
• For example:
integer I1;
• would invoke the first constructor and set both m and n of I1 to zero.
integer I2(20, 40);
• would call the second constructor which will initialize the data members m and n
of I2 to 20 and 40 respectively.
integer I3(I2);
• would invoke the third constructor which copies the values of I2 into I3. It sets the
value of every data element of I3 to the value of the corresponding data element
of I2.
• Such a constructor is called the copy constructor.
• When more than one constructor function is defined in a class, the constructor is
said to be overloaded. 10
Copy Constructor
• A copy constructor is used to declare and initialize an object from
another object. For example:
integer I2(I1);
• The above statement would define the object I2 and at the same time
initialize it to the values of I1. Another form of above statement is
integer I2 = I1;
• The process of initializing through a copy constructor is known as copy
initialization.
• Note that the statement
I2 = I1;
will not invoke the copy constructor. However, if I1 and I2 are objects, this
statement is legal and simply assigns values of I1 to I2, member-by-
member. 11
• A copy constructor takes a reference to an object of the same class itself as
an argument. Example program:
#include<iostream>
using namespace std;
class code
{
int id;
public:
code() { } //constructor 1
code(int a) { id = a; } //constructor 2
code(code &x) //copy constructor
{ id = x.id; //copy in the value
} 12
void display(void)
{ cout << id; }
};
int main()
{
code A(100); //object A is created and initialized
code B(A); //copy constructor called
code C = A; // copy constructor called again
code D; // D is created but not initialized
D = A; // copy constructor not called
14
Destructors
• A destructor is used to destroy the objects that have been created by
a constructor.
• Similar to constructor, a destructor is a member function whose name
is the same as the class name but is preceded by a tilde. For example,
the destructor for the class integer can be defined as shown below:
~integer () { }
• A destructor never takes any argument nor does it return any value. It
will be invoked implicitly by the compiler upon exit from the program
to clean up storage that is no longer accessible.
15
#include<iostream>
using namespace std;
int count = 0;
class alpha
{
public:
alpha()
{ count++;
cout <<“\nNo. of object created ” << count;
}
~alpha()
{ cout <<“\nNo. of object destroyed ” << count;
count--;
}
}; 16
int main()
{
cout <<“\n\nEnter Main\n”;
alpha A1, A2, A3, A4;
{ cout <<“\n\nEnter Block1\n”;
alpha A5;
}
{ cout <<“\n\nEnter Block2\n”;
alpha A6;
}
cout <<“\n\nRe-enter Main\n”;
return 0;
} 17
Enter Main Enter Block2