Constructors in C++
A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class.
Properties of Constructors
- Constructor has same name as the class.
- Constructors don’t have any return type.
- A constructor is automatically called when an object of the class is created.
- If we do not specify any constructor, C++ compiler generates a default constructor for us.It expects no parameters from user and has an empty body.
Types of Constructor
There are three types of constructors defined in C++.
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
1. Default Constructor
Default constructor is the constructor which doesn’t take any argument. It has no parameters.
Example
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y;
//Default Construction Declaration
Myclass()
{
x=10;
y=20;
}
};
void main()
{
clrscr();
//Object creation of class Myclass
Myclass My;
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
getch();
} |
Output
2. Parameterized Constructor
It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.
Parameterized constructors are helpful to provide default value to class object.
Example
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y;
//Parameterized Constructor Declaration
Myclass(int a,int b)
{
x=a;
y=b;
}
};
void main()
{
clrscr();
//Passing values to parameterized constructor
Myclass My(10,20);
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
getch();
} |
Output
3. Copy Constructor
A copy constructor is a member function which initializes an object using another object of the same class.
Example
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y,z,w;
//Declatation of Parameterized Constructor
Myclass(int a,int b)
{
x=a;
y=b;
}
//Declaration of Copy Construction
Myclass(Myclass &m)
{
z=m.x;
w=m.y;
}
};
void main()
{
clrscr();
//Calling Parameterized Constructor
Myclass My(10,20);
//Calling Copy Constructor and passing another object as argument
Myclass My1(My);
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
cout<<"z:"<<My1.z<<"\n";
cout<<"w:"<<My1.w<<"\n";
getch();
} |
Output
Constructor Definition outside the class
Constructor can also be declared outside the class body. It is done using scope resolution operator(::).
Example
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y;
//Declaring Constructor inside class body
Myclass(int a,int b);
};
//Defining Constructor outside class bosy
Myclass::Myclass(int a,int b)
{
x=a;
y=b;
}
void main()
{
clrscr();
Myclass My(10,20);
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
getch();
} |
Output
When class has a lot of member functions and constructors in it then defining constructors outside the class lessens class body. It makes program code easy to understand an it also increases speed of code compilation.
Difference Between Member Function and Constructor
Member Function |
Constructor |
Member functions has a return type. |
Constructors does not have a return type. |
Member function are not called automatically. |
Constructors are automatically called on creation of class object. |
Member functions are not created itself they have to be created by programmer. |
When we do not create constructor compiler creates a default constructor and call itself. |
Multiple constructors of different types can be declared inside a class. Multiple Constructors with same type and same name can also be declared in class which is called constructor overloading.
Destructors in C++
Destructor is a member function which destructs or deletes an object.
A destructor function is called automatically when the object goes out of scope. An object goes out of scope when
- The function ends
- The program ends
- A block containing local variables ends
- A delete operator is called
Apart from normal member functions destructors have same name as the class preceded by a tilde (~) and destructors don’t take any argument and don’t return anything.
In a class there can only be one destructor.
Why user defined destructor
If we do not write our own destructor in class, compiler creates a default destructor for us. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid memory leak.
Example
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y;
//Default Construction Declaration
Myclass()
{
x=10;
y=20;
}
//Defining destructor inside class Myclass
~Myclass()
{
cout<<"\nDestructor is called\n";
getch();
}
};
void main()
{
clrscr();
//Object creation of class Myclass
Myclass My;
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
getch();
} |
Output
x:10
y:20
Destructor is called |
For more Object Oriented Programming Lab Experiments Click Here