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

Before creating a constructor, always remember the below points.

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

Copy Constructor

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

Example

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

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.

Example

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

Summary

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