0% found this document useful (0 votes)
24 views14 pages

Constructor and Destructor

Uploaded by

jatinrastogi81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

Constructor and Destructor

Uploaded by

jatinrastogi81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Programming using

C++
Constructor and Destructor
 Constructor
 Default Constructor
 Parametrized Constructor
 Copy Constructor
 Constructor with default arguments
 Constructor with other class object as argument
 Destructor Concept
OUTLINE
Constructors in Class

• Constructor is a special member function of the class whose name is same as the class name.
• It doesn't have a return type not even void.
• It is automatically called when an object is first created.
• It is used for automatic initialization of objects.
• It is generally declared in the public section.
• Incase the programmer has not included a constructor for the class, the compiler provides a default constructor for
the class (does not take any argument), in which case it does not initialize the object rather only creates an object.
Default Constructor

#include<iostream> Output:
using namespace std;
Default Constructor called
Default Constructor called
class Complex
0 +i0
{int real, img;
public:
Complex()
{cout<<"Default Constructor called\n";
real=0; img=0;}
void show(){cout<<real<<" +i"<<img<<endl;}
};
int main() Default constructor doesn't take any
{Complex c; //Default constructor called argument. The compiler doesn't
//automatically. Implicit calling. provide a default constructor, once
Complex c1=Complex(); //explicit constructor called
you declare a constructor of your own.
c.show();
}
Parameterized Constructors
#include<iostream>
Output:
using namespace std;
class Complex Parameterized Constructor called
{int real, img; Parameterized Constructor called
public: 2 +i3
Complex(int i, int j)
{cout<<"Parameterized Constructor called\n";
real=i;
img=j;
}
void show(){cout<<real<<" +i"<<img<<endl;}
};
int main()
{Complex c(2,3); //Default
//constructor called automatically. Implicit calling
Complex c1=Complex(2,3); //explicit constructor called
c.show(); Parameters are passed incase of
} parametrized constructor
Predict the following output
#include<iostream> Output:
using namespace std;
class Complex
Compile time error
{
int real, img;
public:
Complex(int i, int j)
{cout<<"Parameterized Constructor called\n";
real=i;
img=j;
}
void show(){cout<<real<<" +i"<<img<<endl;}
};
int main()
{
Complex c(2,3);
Complex c1; Reason: complex c1 will create error as
c.show(); it needs a default constructor which is
} not declared by the user and the
compiler will not provide a default
constructor as a constructor is already
declared.
Predict the following output
#include<iostream> Output:
using namespace std;
class Vector
12645
{int size;
int *arr;
public:
Vector(){size=0;}
Vector(int size, int *p)
{this->size=size;
arr=new int[this->size];
Reason:
for(int i=0;i<size;i++) The memory for v.arr and v1.arr is
arr[i]=p[i]; same. Any changes inside v1.arr will
} be reflected for v as well.
void change(int i, int value)
{arr[i]=value;}
void show()
{for(int i=0;i<size;i++)
cout<<arr[i]<<" "; Solution:
} The solution is copy constructor
}; where the same class object is passed
int main()
{int a[]={1,2,3,4,5};
as argument to copy from one object
Vector v(5,a); to another.
Vector v1=v;
v1.change(2,6);
v.show();
}
Copy Constructor
#include<iostream> int main()
using namespace std; {int a[]={1,2,3,4,5};
class Vector Vector v(5,a);
{int size, *arr; Vector v1=v;
public: v1.change(2,6);
Vector(){size=0;} v.show();
v1.show();}
Vector(int size, int *p){this->size=size;
arr=new int[this->size];
for(int i=0;i<size;i++) arr[i]=p[i];}

Vector (Vector &o) {size=o.size;


arr=new int[size];
for(int i=0;i<size;i++) arr[i]=o.arr[i];}

void change(int i, int value)


{arr[i]=value;}
Output:
void show() 12345
{for(int i=0;i<size;i++) 12645
cout<<arr[i]<<" ";
cout<<"\n";}
}; In copy constructor the argument is
reference of the object.
Predict the following output
#include<iostream> Output:
using namespace std;
class Complex
Parameterized Constructor called
{ Copy Constructor called
int real, img; 2 +i3
public:
Complex(){ }
Complex(int i, int j): real{i}, img{j}
{cout<<"Parameterized Constructor called\n";}

Complex(Complex &o): real{o.real}, img{o.img}


{cout<<"Copy Constructor called\n";}

void show(){cout<<real<<" +i"<<img<<endl;}


};
int main()
{ Reason:
Complex c(2,3); Complex c1=c invokes copy constructor
Complex c1=c; //or can be written as Complex c1(c); Whereas,
Complex c2;
c2=c; c2=c doesn't invoke copy constructor
c2.show(); as this initialization is done by =
} operator which copies bit-by bit from c
to c2.
Default arguments in Constructor
#include<iostream> Output:
using namespace std;
class Complex
0 +i0
{int real, img; 3 +i0
public:Complex(int i=0, int j=0):real{i},img{j} 2 +i5
{ }
void show(){cout<<real<<" +i"<<img<<endl;}
};
int main()
{Complex c1;
c1.show();
Complex c2(3);
c2.show();
Complex c3(2,5);
c3.show();
}
Constructor taking argument from other class object
#include<iostream>
using namespace std;
class Distance1
{int km,m;
public:
Distance1(int a, int b){km=a; m=b;}
int get_km(){return km;}
int get_m(){return m;}
};
class Distance2
{int km,m,cm;
public:
Distance2(Distance1 d)
{km=d.get_km();
m=d.get_m();
cm=0;}
void show()
{cout<<km<<"km "<<m<<"m "<<cm<<"cm\n"; }
};
int main()
{Distance1 d1(2,300);
Distance2 d2(d1);
d2.show();
}
Destructor
• Destructors are used to destroy the objects created by the constructor. Output:
• They are automatically called when the object goes out oof scope.
• ~ClassName()
Destructor called for object with i= 4
• They don't take any parameters. Destructor called for object with i= 3
Destructor called for object with i= 2
Destructor called for object with i= 1
#include<iostream>
using namespace std;
class Integer
{int i;
public:
Integer(int a){i=a;}
~Integer(){cout<<"Destructor called for object with
i= "<<i<<"\n"; }
};
int main()
{Integer i1(1),i2(2),i3(3),i4(4);}
Destructor
#include<iostream> Output:
using namespace std;
class Vector
Inside destructor
{int size; Inside destructor
int *arr;
public:
Vector(){size=0;}
Vector(int size, int *p)
{this->size=size;
arr=new int[this->size];
for(int i=0;i<size;i++)
arr[i]=p[i];
}
~Vector(){cout<<"Inside destructor\n";
delete []arr;}
};
int main()
{int a[]={1,2,3,4,5};
int b[]={10,20,30};
Vector v1(5,a);
Vector v2(3,b);
}
Predict the output(Quiz)
Q2.
Q1. #include<iostream>
#include<iostream> using namespace std;
using namespace std; class Integer1
class Complex {public:
{int real, img; int j;
public: Intege1r(){ }
Complex():real{0},img{0} Integer1(int a){j=a;}
{cout<<"Inside default constructor\n"; } ~Integer1(){cout<<"Destructor called for
Complex(int i=0, int j=0):real{i},img{j} Integer1 object with j= "<<j<<"\n"; }
{cout<<"Inside parameterized constructor\n";} };
void show(){cout<<real<<" +i"<<img<<endl;} class Integer
}; {int i;
int main() public:
{Complex c1; Integer(){ }
c1.show(); Integer(int a){i=a;}
} Integer(Integer1 o){i=o.j;}
~Integer(){cout<<"Destructor called for
Integer object with i= "<<i<<"\n"; }
};
int main()
{Integer1 i1(1);
Integer i2=i1;}

You might also like