Constructor and Destructor in C++ class

Constructor and Destructor in C++ class

While making any class is C++, we must know about the constructors and destructors. They are the important part of the classes every programmer must know.


Constructors in C++

1. Constructor is a member function of a class.
2. The name of the constructor is same as the name of the class.
3. It has no return type i.e., we cannot use return keyword.
4. It must be an instance member function i.e., it can never be static.
5. It is implicitly invoked when an object is created.
6. It used to solve the problem of initialization.
7. The constructor must called whenever any object of a class is created.


For Example:
#include<iostream>
using namespace std;

class Complex
{
public:
  Complex()
  {
    cout << "Hello Friends!, I am a Constructor" << endl;
  }
};

int main()
{
  Complex C1, C2, C3;      //Constructor called 3 times.
}
Output:
Hello Friends!, I am a Constructor
Hello Friends!, I am a Constructor
Hello Friends!, I am a Constructor

Types of Constructors in C++

1. Default Constructor.
2. Parameterized Constructor.
3. Copy Constructor.


Default Constructor

The constructor having no parameter is called default constructor.


Parameterized Constructor

The constructor having some parameter is called parameterized constructor.


For Example:
#include<iostream>
using namespace std;

class Complex
{
private:
  int real, imaginary;
 
public:
  Complex()
  {
    cout << "Hello Friends!, I am a Default Constructor" << endl;
  }
  
  Complex(int num1)
  {
    real = num1;
    cout << "Hello Friends!, I am a Parameterized Constructor" << endl;
  }
  
  Complex(int num1, int num2)
  {
    real = num1;
    imaginary =  num2;
    
    cout << "Hello Friends!, I am a Parameterized Constructor" << endl;
  }
};

int main()
{
  Complex C1;           //Default Constructor called.
  
  //Parameterized Constructor having 1 parameter will be called
  Complex C2(2);       
  
  //Parameterized Constructor having 2 parameters will be called
  Complex C3(9,2);
}
Output:
Hello Friends!, I am a Default Constructor
Hello Friends!, I am a Parameterized Constructor
Hello Friends!, I am a Parameterized Constructor

Copy Constructor

1. Copy constructor is overloaded constructor used for initializing an object with another object of the same class.

2. It is called when we initialize the object with another object of same class. e.g., Person p1=p2; where Person is a class.

There are two types of copy constructor.

1. Default Copy Constructor: Default copy constructor is defined by the compiler when object is created and there is no copy constructor defined in the program.

2. User-defined Copy Constructor: The programmer defines the user-defined copy constructor in the program.


Points to Remember:-

1. If there is no constructor defined by the programmer in the program, the compiler defines two constructors (default constructor and copy constructor).

2. If the programmer defined any constructor (except copy constructor) in the program then the compiler do not define default constructor but it defines copy constructor.

3. If the programmer defined copy constructor in the program then the compiler do not define any constructor.


For Example:
#include<iostream>
using namespace std;

class Complex
{
private:
  int real, imaginary;
  
public:
  Complex()
  {
    cout << "Hello Friends!, I am a Default Constructor" << endl;
  }
  
  Complex(int num1)
  {
    real = num1;
    cout << "Hello Friends!, I am a Parameterized Constructor" << endl;
  } 
  
  Complex(int num1, int num2)
  {
    real = num1;
    imaginary =  num2;
    
    cout << "Hello Friends!, I am a Parameterized Constructor" << endl;
  }
  
  Complex (Complex &C)       //Copy Constructor.
  {
    real = C.real;
    imaginary = C.imaginary;
  }
};

int main()
{
  Complex C1;           //Default Constructor called
  
  //Parameterized Constructor having 1 parameter will be called
  Complex C2(2);
  
  //Parameterized Constructor having 2 parameters will be called
  Complex C3(9,2);
    
  Complex C4(C3);       //Copy Constructor called.
  
  Complex C5 = C4;      //Copy Constructor called.
}
Output:
Hello Friends!, I am a Default Constructor
Hello Friends!, I am a Parameterized Constructor
Hello Friends!, I am a Parameterized Constructor

Destructors in C++

1. Destructor is an instance member function of a class.
2. The name of destructor is same as the name of the class but preceded by tilde (~) symbol.
3. Destructor can never be static.
4. It has no return type.
5. It takes no argument i.e., no overloading is possible in destructor.
6. It is invoked implicitly when object is going to destroy.
7. It should be defined to release the resources allocated to an object.


For Example: Destructors in C++
#include<iostream>
using namespace std;

class Complex
{
public:
  Complex()
  {
    cout << "Hello Friends!, I am a Constructor" << endl;
  }
 
  ~Complex()
  {
    cout << "Hello Friends!, I am a Destructor" << endl;
  }
};

int main()
{
  Complex C1, C2, C3;      //Constructor & Destructor called 3 times
}
Output:
Hello Friends!, I am a Constructor
Hello Friends!, I am a Constructor
Hello Friends!, I am a Constructor
Hello Friends!, I am a Destructor
Hello Friends!, I am a Destructor
Hello Friends!, I am a Destructor



<< Previous                                                                                      Next >>
Previous
Next Post »