Constructor Destructor
Constructor Destructor
Constructor Destructor
&
Destructors
Constructors
• Constructor is a special member function whose name is
same as that of the class in which it is defined and it does not
return anything (not even void).
• Constructor’s primary task is to allocate memory for the
object and as a secondary task it may initialize the data
members of the object.
class X
{
int a;
public: main()
{
X() X x1=X(); // Explicit Call
{ x1.disp();
a=5;
} X x2(); // Implicit Call
void disp() x2.disp();
{ }
cout<<a;
}
};
• Constructor with no arguments is known as the default
constructor
• If the user does not specify the constructor then
compiler provides a default constructor into the class.
X( )
{
// no statement inside the constructor
}
class A
{
int x,y; main()
{
public: A obj1=A(5,2); // Explicit call
obj.show(); // 7
A(int a,int b)
{
x=a; A obj2(10,20); //Implicit Call
y=b; obj2.show(); // 30
}
A obj3=(4,6); //Error
void show() obj3.show();
{
A obj4=4,6; //Error
cout<<“sum is”<<x+y; obj4.disp();
}
}; }
Constructor Overloading
More than one constructors inside a class which can be
differentiated by the compiler at the compile time based on
the no of args, type of args or sequence of args , is called as
Constructor Overloading.
Constructor Overloading Example
class X
{
int a,b; main ()
public: {
X()
{ X x1=x(5);
a=b=0; x1.disp(); // 5, 5
}
X(int c,int d) X x2(10,20);
{
a=b=c; x2.disp(); // 10,20
}
X(int c,int d) X x3; //0,0
{ x3.disp();
a=c; b=d;
}
void disp() X3=x2 //10,20
{ x3.disp();
cout<<a<<b; }
}
};
Copy Constructor
It is used to copy the content of one object to another object
of same class.
Copy constructor takes a reference type as argument
otherwise it leads to an infinite loop.
Compiler provides a default copy constructor if the
programmer has not specified it explicitly.
•
Copy Constructor Example
class X
{
int a;
main()
public:
X(int k) {
{ X x1(5);
a=k; X X2(x1); //calls the copy constructor
}
X(X &p) //Copy Constructor
{ X1.disp(); // 5
a=p.a; X2.disp(); // 5
}
void disp() }
{
cout<<a;
}
};
Default arguments in Constructor
The parameterized constructor which is having one or more
than one default arguments is referred as constructor with
default argument.
class X
{
int a,b; main()
{
public:
X(int c,int d=0) X x1(5,7);
{ X x2(1);
a=c;
b=d; x1.disp(); // 5,7
}
void disp() x2.disp(); // 1,0
{
cout<<a<<b; }
}
};
Ambiguity in Constructor
class A
{
int x; main()
public: {
A() // Invokes obj1 A obj1;
{ obj1.disp(); // No ambiguity
cout<<“Hello”;
}
A(int a=5) A obj2(10);
{ obj2.disp();
x=a;
} }
void disp()
{
cout<<x;
} Error :A::A();
}; A::A(int);
Dynamic Constructor
The constructors can also be used to allocate while creating
objects. This will enables the system is allocate right
amount of memory for each object whenever the objects
are not of the same size , thus resulting in saving of memory
& allocation of memory to object at the time of their
constructor is known as Dynamic Constructor.
Dynamic Constructor Example
class A
{
char *name;
int len;
public: main()
A() {
{ A n1(“James”),
len=0; n2(“Martyn”),
name=new char[len+1]; n3(“”Sachin”),
} ob1,ob2;
A(char *s)
{ ob1.join(n1,n2);
len=strlen(s); ob2.join(ob1,n3);
name=new char[len+1]; n1.display(); // James
strcpy(name,s); n2.display(); // Martyn
} n3.display(); // Sachin
void display() ob1.display(); // JamesMartyn
{ ob2.display(); // JamesMartynSachin
cout<<name;
} }
void join(A &a, A &b)
{
len=a.len+b.len;
name=new char[len+1];
strcpy(name,a.name);
strcat(name,b.name);
}
};
Destructor
It de-allocates the memory of the object which was allocated by a
constructor.
Destructors are called automatically when the object goes out of scope.
The name of the destructor is same as class name and must be
preceded by a tilde mark(~).
int i=0;
class X main()
{ {
int a; X x1,x2;
public: }
X() Output:
{ CON1
cout<<“CON”<<++i; CON2
} DES2
~x() DES1
{
Destructors are called in the reverse order of
cout<<“DES”<<i--;
constructors.
}
};
int i=0;
class X main()
{ {
int a; X x1,x2;
public: {
X() X x3,x4;
{ }
cout<<“CON”<<++i; {
} X x5;
~x() }
{ }
cout<<“DES”<<i--;
} Output:
}; CON1
CON2
CON3
CON4
DES4
DES3
CON3
DES3
DES2
DES1
Properties of Destructor
Name of the destructor is same as the class name
preceded by ~ mark.
It does not have any return type, not even void.
It can’t be inherited but the derived class may call the
base class destructor.
We can have only one destructor in a class i.e.
Destructor can’t be overloaded as it does not take any
argument.
We can’t refer to the address of the destructor.
Unlike constructor, it can be virtual.
Object having a destructor can’t be made as a member
of union.