Constructor, Types Of Constructor And Destructor In C++

Introduction

In this blog, we will learn about the constructor, types of constructor, and the destructor in C++ language.

Let's start!

What is constructor?

C++ compiler provides a special kind of member function for 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.

  • Constructor name is 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.

Example

  1. // default constructor example  
  2. #include<iostream.h>  
  3. #include<conio.h>  
  4. class emp  
  5. {  
  6.     int id,sal; //member variable  
  7.     public:  
  8.         emp() //default constructor  
  9.         {  
  10.             id=1;  
  11.             sal=2000;  
  12.         }  
  13.         void display() // member function for display data  
  14.         {  
  15.             cout<<"Employee id: "<<id<<endl;  
  16.             cout<<"Employee salary: "<<sal<<endl;  
  17.         }  
  18. };  
  19. void main()  
  20. {  
  21.     emp obj;  
  22.     obj.display();  
  23.     getch();  
  24. }  

Parameterized Constructor

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

Example

  1. //parameterized constructor  
  2.  
  3. #include<iostream.h>  
  4. #include<conio.h>  
  5. class emp  
  6. {  
  7.     int id,sal; //member variable  
  8.       public:  
  9.         emp(int empId,int empSal) //parameterized constructor  
  10.         {  
  11.             id=empId;  
  12.             sal=empSal;  
  13.         }  
  14.         void display() //member function  
  15.         {  
  16.             cout<<"Employee id: "<<id<<endl;  
  17.             cout<<"Employee salary: "<<sal<<endl;  
  18.         }  
  19. };  
  20. void main()  
  21. {  
  22.     emp obj(1,3000); //passing constructor argument value  
  23.     obj.display();  
  24.     getch();  
  25. }  

Copy Constructor

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

Example

  1. //copy constructor  
  2.  
  3. #include<iostream.h>  
  4. #include<conio.h>  
  5. class emp  
  6. {  
  7.     int id,sal;  
  8.     public:  
  9.         emp(int empId,int empSal) //paramereized constructor  
  10.         {  
  11.             id=empId;  
  12.             sal=empSal;  
  13.         }  
  14.         emp(emp &em)    //copy constructor  
  15.         {  
  16.             id=em.id;  
  17.             sal=em.sal;  
  18.         }  
  19.         void display() //member function  
  20.         {  
  21.             cout<<"\nEmployee id: "<<id<<endl;  
  22.             cout<<"Employee salary: "<<sal<<endl;  
  23.         }  
  24. };  
  25.   
  26. void main()  
  27. {  
  28.     emp obj(1,2000);  
  29.     emp obj1(obj);  
  30.   
  31.     obj.display();  
  32.     obj1.display();  
  33.     getch();  
  34. }  

What is destructor?

A destructor is used for destroying the objects that have been created by the constructor.

Before creating a destructor, always remember the below points.

  • Destructor name is same as the class name and is prefixed with tilde (~).
  • It can neither return a value nor can it take any parameter.

Example

  1. // destructor example  
  2.  
  3. #include<iostream.h>  
  4. #include<conio.h>  
  5. class emp  
  6. {  
  7.     int id,sal;  
  8.     public:  
  9.         emp()  //default constructor  
  10.         {  
  11.             id=1;  
  12.             sal=5000;  
  13.         }  
  14.         ~emp()    //destructor  
  15.         {  
  16.             cout<<"Ok Bye for now...";  
  17.         }  
  18.         void display() //member function  
  19.         {  
  20.             cout<<"Employee id: "<<id<<endl;  
  21.             cout<<"Employee salary: "<<sal<<endl;  
  22.         }  
  23. };  
  24. void main()  
  25. {  
  26.     emp obj;  
  27.     obj.display();  
  28.     getch();  
  29. }  

Summary

In this blog, I covered constructor, constructor types, and destructor in C++ language. If you face any problem, please comment. Thanks for reading.