Constructor and Destructor in C++ || Constructor || Destructor || || OOPs Concept || C++



Constructors in C++ : 

  • Constructor is a special member function of a class.
  • A Constructor is always created inside the public section of a class.
  • A Constructor has the same name as that of the Class name .
  • A constructor can be with parameters or without parameter.  
  • Since Constructors can have parameters it can also be overloaded as we do with functions.
  • It is automatically called when an object of a class is created. 
  • It does not have a return type.
  • A Class can have many Constructors.

Syntax for Constructors: 


//class created 

class A 	
{
    public:
          A()		// Constructor created 
          {
              //stmt;
          }
}


Example of Simple Constructor Program: 


#include <iostream>
#include <stdlib.h>
using namespace std;

class construct
{
public:
    construct()
    {
        cout << "Congrats, You have successfully called a constructor";
      
    }
};

int main()
{
    construct p;
    return 0;
}

OUTPUT:-

What is Constructor Overloading ?

Constructor Overloading means a Constructor with same name but different parameters / arguments. It is similar to that of Function Overloading

Example of Constructor Overloading :- 


// C++ program to demonstrate constructor overloading
#include <iostream>
using namespace std;

class Room {
   private:
    double length;
    double breadth;

   public:
    // First Constructor 
    Room() {
        length = 6.9;
        breadth = 4.2;
    }

    // Second Constructor 
    Room(double l, double b) {
        length = l;
        breadth = b;
    }
    // Constructor Overloaded
    Room(double len) {
        length = len;
        breadth = 7.2;
    }

    double calculateArea() {
        return length * breadth;
    }
};

int main() {
    Room room1, room2(8.2, 6.6), room3(8.2);

    cout << "When no argument is passed: " << endl;
    cout << "Area of room = " << room1.calculateArea() << endl;

    cout << "\nWhen (8.2, 6.6) is passed." << endl;
    cout << "Area of room = " << room2.calculateArea() << endl;

    cout << "\nWhen breadth is fixed to 7.2 and (8.2) is passed:" << endl;
    cout << "Area of room = " << room3.calculateArea() << endl;

    return 0;
}

OUTPUT:-

Destructor in C++ :-

  • Destructor is also a special member function of a class and it is also defined inside the public block of any class. 
  • It also have the same name as that of class name but with a tilde sign (~) this is the notation of destructor.
  • Destructors can not have parameters.
  • Hence Destructor cannot be overloaded.
  • It is used to destroy the object / instance of the class which means destructor is called at the end in order to end / destroy the object / instance the class.
  • Destructors also does not have return type as like as Constructors.
  • A class can have many Constructors but only one Destructor.

Syntax for Constructors: 


//class created 

class A 	
{
    public:
          A()		// Constructor created 
          {
              //stmt;
          }
          ~A()		// Destructor created
          {
          	  //stmt;
          }
          
}

Example of Destructor Program:



#include <iostream>  
using namespace std;  
class Employee  
 {  
   public:  
        Employee()    
        {    
            cout<<"Constructor Invoked"<<endl;    
        }    
        ~Employee()    
        {    
            cout<<"Destructor Invoked"<<endl;    
        }  
};  
int main(void)   
{  
    Employee e1; //creating an object of Employee   
    Employee e2; //creating an object of Employee  
    return 0;  
}  

OUTPUT:-




#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post