Constructor and Destructor
Constructor and Destructor
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];}