Constructor, types Of constructor and destructor in C++

In this blog, we will learn about the constructor, types of the constructor, and the destructor in C++ language. Let’s start!

What is a constructor?

C++ compiler provides a special kind of member function for the initialization of objects. This function is called the constructor.

Types of constructor

  • Default constructor
  • Parameterized constructor
  • Copy constructor

Before creating a constructor, always remember the below points.

  • The constructor name is the same as the class name.
  • It is declared with no return types (int, float, double, and not even void).
  • It is declared in the public section.
  • It is invoked automatically when the objects are created.

Default constructor

A constructor that accepts no parameter is called the default constructor.

// default constructor example  
#include<iostream.h>  
#include<conio.h>  
class emp  
{  
    int id,sal; //member variable  
    public:  
        emp() //default constructor  
        {  
            id=1;  
            sal=2000;  
        }  
        void display() // member function for display data  
        {  
            cout<<"Employee id: "<<id<<endl;  
            cout<<"Employee salary: "<<sal<<endl;  
        }  
};  
void main()  
{  
    emp obj;  
    obj.display();  
    getch();  
}  

Parameterized constructor

A constructor that takes at least one argument is called the parameterized constructor.

//parameterized constructor  
 
#include<iostream.h>  
#include<conio.h>  
class emp  
{  
    int id,sal; //member variable  
      public:  
        emp(int empId,int empSal) //parameterized constructor  
        {  
            id=empId;  
            sal=empSal;  
        }  
        void display() //member function  
        {  
            cout<<"Employee id: "<<id<<endl;  
            cout<<"Employee salary: "<<sal<<endl;  
        }  
};  
void main()  
{  
    emp obj(1,3000); //passing constructor argument value  
    obj.display();  
    getch();  
}  

Copy constructor

A constructor used for initializing an object from another object is called a copy constructor.

//copy constructor  
 
#include<iostream.h>  
#include<conio.h>  
class emp  
{  
    int id,sal;  
    public:  
        emp(int empId,int empSal) //paramereized constructor  
        {  
            id=empId;  
            sal=empSal;  
        }  
        emp(emp &em)    //copy constructor  
        {  
            id=em.id;  
            sal=em.sal;  
        }  
        void display() //member function  
        {  
            cout<<"\nEmployee id: "<<id<<endl;  
            cout<<"Employee salary: "<<sal<<endl;  
        }  
};  
  
void main()  
{  
    emp obj(1,2000);  
    emp obj1(obj);  
  
    obj.display();  
    obj1.display();  
    getch();  
}  

What is a destructor?

A destructor is used to destroy the objects that have been created by the constructor.

Before creating a destructor, always remember the below points

  • The destructor name is the same as the class name and is prefixed with a tilde (~).
  • It can neither return a value nor can it take any parameter.
// destructor example  
 
#include<iostream.h>  
#include<conio.h>  
class emp  
{  
    int id,sal;  
    public:  
        emp()  //default constructor  
        {  
            id=1;  
            sal=5000;  
        }  
        ~emp()    //destructor  
        {  
            cout<<"Ok Bye for now...";  
        }  
        void display() //member function  
        {  
            cout<<"Employee id: "<<id<<endl;  
            cout<<"Employee salary: "<<sal<<endl;  
        }  
};  
void main()  
{  
    emp obj;  
    obj.display();  
    getch();  
}  

Leave a Reply

Your email address will not be published.