Constructors and Destructors
Constructors and Destructors
Constructors
• When a variable of built in data type goes out of
scope, the compiler automatically destroys the
variable.
Int i= 10;
char c= ‘a’;
But this does not happen in case of objects.
• So, C++ provides a special member function
called the constructor which enables an object
to initialize itself when it is created.
• This is known as automatic initialization of
objects
Contd..
•“A constructor is a special member function whose task is to
initialize the objects of its class.”
•Its name is same as that of the class.
•It is invoked whenever an object of its associated class is created.
•Example:
Class sum //constructor defined
{ sum::sum(void)
inti, j; {
i=10;
public:
j=20;
sum(void);
}
//constructordeclared sums1; //objects1created
};
Default Constructor
• A constructor that accepts no parameters is called
the default constructor.
• The default constructor for class A is
A::A( )
• If no such constructor is defined, then compiler
itself supplies a default constructor.
• So, a statement such as
A a;
• Invokes the default constructor of the compiler to
create the object a.
Example
#include <iostream>
using namespace std;
class A
{ public:
A()
{cout<<"Default Constructor called"<<endl;}
};
int main()
{
A a1;
A a2;
return 0;
}
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 so they can not return values.
• They can not be inherited, though the derived
class can call a base class constructor.
• They can have default arguments.
• Constructors can not be virtual.
Parameterized Constructor
• Constructors that take arguments are called
parameterized constructors
void Display()
{ cout<<"Values
“<<a<<"\t"<<b; }
};
Constructors with default arguments
• It is possible to define constructors with default
arguments
complex (float real, float imag=0);
• The default value of imag is zero.
• So, if called via:
complex C(5.0);
Here, imag is by default 0.0
• If called:
complex C(2.0, 3.0);
Here, imag value will be 3.0, as this value will override
the default value 0.0
Example
class student
int main()
{
{
int roll_no;
student s1;
float marks;
student s2(1,60);
public:
cout<<"Output using default
student(int x=10,float y=40) constructor arguments:\n";
{ s1.display();
roll_no=x; cout<<"Output without default
marks=y; constructor arguments:\n";
} s2.display();
void display() };
{
cout<<"Roll no:"<<roll_no<<endl;
cout<<"Marks:"<<marks<<endl;
}
};
Copy Constructor
• A constructor that can accept a reference to its
own class as a parameter, is called a copy constructor.
• Is used to declare and initialize an object from another
object.
Eg:
sum s2 (s1);
sum s2 = s1;
• The process of initializing through a copy constructor is
known as copy initialization.
• However,
sum s2 = s1; & s2 = s1;
are not same
Contd..
• The statement
s2=s1;
• Will not invoke the copy constructor.
• If s1 and s2 are objects ,this statement is legal
and assigns the values of s1 to s2, member-by-
member.
Example
class abc int main()
{ private: { abc ob1(50,4.5, 'a'); // object created
int a; abc ob2(ob1);
float b; abc ob3=ob2;
char c; cout<<"object 1 data value"<<endl;
public: ob1.display();
abc(int x,float y, char z) cout<<"object 2 data value"<<endl;
{ a=x; ob2.display();
b=y; cout<<"object 3 data value"<<endl;
c=z; ob3.display();
} }
abc(abc &p);// copy contructor
void display()
{ cout<<"a= "<<a<<endl;
cout<<"b= "<<b<<endl;
cout<<"c= "<<c<<endl; }
};
abc:: abc(abc &p)
{
a=p.a;
b=p.b;
c=p.c;
}
Multiple Constructors
};
int main( )
{
B *ob=new B();
delete ob;
}