Constructor and Destructors

Constructing Objects
Automatic initialization
Constructors
Parameterized constructors
Constructor overloading
Default constructor
Default argument Constructor
Dynamic constructor
Constructing matrix objects
Destructor

Introduction
• C++ is to create user-defined data type as class behave very similar
to the built-in type
• Able to initialize a class type variable(object) when it is declared
same as intialization of an ordinary variable
• Ex: int m=20;
• float x=79.8;
• Are valid initialization statements for basic data types
• When a varaible of built-in type goes out of scope, the compiler
automatically destroyed the variable.
“C++ provides a special member function called Constructor which
enables an object to initialize itself when it is created known as
“automatic initialization of objects” “
“Destructor that destroys the objects when they are no longer
required”


Constructors
1.A constructor is a “Special” member function whose task is to
initialize the objects of its class
2. It is special because its name is the same as the class name
3.The constructor is invoked whenever an object of its associated class
is created.
“Called constructor because it constructs the value of data members
of the class”
Syntax:
//class with a constructor
Class integer
{ int m,n;
public:
integer(void); //Constructor declared
………..
……………
};
Continues..
integer::integer(void)//constructor defined
{ m=0;n=0;
}

integer int1; //object int1 created
not only creates the object int1 of type integer but also initializes its
data members m and n to zero. There is no need to write any statement
to invoke the constructor function. If a normal member function is
defined for zero initialization, we would need to invoked this function for
each of the objects separately. This is very inconvenient ,if there are
a large number of objects

Default Constructor
“A constructor that accepts no parameters is called the default
constructor”
The default constructor class A is A::A().
If no such constructor is defined ,then the compiler supplies a default
constructor. such as
A a;
invokes the default constructor of the compiler to create the object a.

Constructor functions, special
characteristics
 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, through a derived class can call the base class
constructor.
 Like other c++ functions, they can have default arguments
 Constructors cannot be virtual
 We cannot refer to their addresses.
 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
Note: When a constructor is declared for a class, initialization of the class
objects becomes mandatory

Parameterized Constructor
C++ permits us to achieve objective by passing arguments to the constructor function
when the objects are created. The constructors that can take arguments are called
Parameterized Constructors

The constructor integer() modified to take arguments
class integer
{ int m,n;
public :
integer(int x,int y);// parameterized constructor
………
};
integer::integer(int x,int y)
{ m=x;n=y;
}
When a constructor has been parameterized, integer int1; may not work
We must pass the initial values as arguments to the constructor function when an object
is declared.

Continues….
This can be done in two ways
1. By calling the constructor explicitly
2. By calling the constructor implicitly
First Method:
integer int1=integer(0,100);//explicit call
The above statement creates an integer object int1 and passes the
value 0 and 100 to it.
Second Method
integer int1(0,100);//implicit call (shorthand method)

//Simple program :Class with constructor
#include<iostream.h>
class integer
{ int m,n;
public:
//constructor declared
integer(int,int);
void display(void)
{ cout<<“m=“<<m<<endl;
cout<<“n=“<<n<<endl;
}
};
//constructor defined
integer:: integer(int x,int y)
{ m=x;n=y;
}


int main()
{//Constructor called implicitly
intege int1(0,100);
//Constructor called explicitly
Integer int2=integer(55,65);
cout<<“\n object1=“<<endl;
int1.display();
cout<<“\n object2=“<<endl;
int2.display();
return 0;
}
Output
Object1
m=0
n=100
Object2
m=55
n=65
Constructor functions defined
//inline functions
class integer
{int m,n;
public:
//inline constructor
integer(int x,int y)
{ m=x;y=n;
}
….
…..
};

//Constructor can accept a
//reference to its own class as
// parameter
class A
{ ….
….
public:
A(A&);
};

The parameter of a constructor can be of any type except that of the
class to which it belongs
class A
{
….
….
public:
A(A);
};
is illegal
Multiple Constructor in a class
• Two kinds of constructor
integer();//no arguments
integer(int,int);//two arguments
C++ permits us to use both
constructor in the same class
class integer
{ int m,n;
public:
//constructor 1
integer(){m=0;n=0;}
//constructor 2
integer(int a,int b)
{ m=a;n=b;}
//constructor 3
integer(integer &i)
{m=i.m;n=i.n;}

};


invoke constructor
First method
//constructor receives no arguments
//set both m and n of i1 to zero
integer i1;
Second method
//constructor receives two arguments
//set m and n of i2 to 20 and 40
Integer i2(20,40);
Third method
//constructor copies the values of i2 to i3
//sets the value of every data element of
//i3 to the value of the corresponding
//data element of i2(copy constructor)
Integer i3(i2);

Note: When more than one constructor
function defined in a class, we say
constructor is overloaded


Overloaded constructors :add two complex numbers
#include<iostream.h>
class complex
{float x,y;
public:
complex(){ }
complex(float a){x=y=a;}
complex(float real,float imag)
{ x=real;y=imag;}
friend complex sum(complex,complex);
friend void show(complex);
};
complex sum(complex c1,complex c2)
{complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return (c3);
}
void show(complex c)
{ cout<<c.x<<“+j”<<c.y<<endl;}
int main()
{ complex A(2.7,3.5);
complex B(1.6);
complex C;
c=sum(A,B);

cout<<“A=“;show(A);
cout<<“B=“;show(B);
Cout<<“C=“;show(C);
//Another way to give initial values
//second method
complex P,Q,R;
P=complex(2.5,3.9);
Q=complex(1.6,2.5);
R=sum(P,Q);
cout<<endl;
cout<<“P=“;show(P);
cout<<“Q=“;show(Q);
cout<<“R=“;show(R);
return 0;
}
Output
A=2.7+J3.5 P=2.5+J3.9
B=1.6+J1.6 Q=1.6+J2.5
C=4.3+J5.1 R=4.1+J6.4