Constructor and Destructor in C++

Constructor and Destructor in C++

The constructor is used to allocate the memory if required and constructs the object of the class. Whereas, a destructor is used to perform required clean-up when an object is destroyed . The destructor is called automatically by the compiler when an object gets destroyed. Here, on this page, we will discuss Constructors and Destructors in C++.

Constructor and Destructor in C++

Constructor in C++

  • When we create an object and don’t assign values the data of that object take up garbage values.
  • To make sure that this doesn’t happen we use constructors to assign values to them as soon as they are created or memory is assigned.

The new version of C++ automatically calls a default constructor implicitly to assign a default value in cases when we don’t create a constructor ourselves.

Constructor and Destructors in C++

Syntax –

A constructor can be declared –

  1. Inside the class
  2. Outside the class (using scope resolution :: operator)

More information

  1. Constructors have the same name as class name
  2. They are called using classname and (); example myClass();
  3. They can also be defined(initialised) outside class by using the class name and scope resolution :: operator.

Example Syntax

Destructors in C++

We worked with initialising the object values, but what when the scope of the object ends?

We must also destroy the object, right ! For this we use Destructors in C++. We can create our own destructor as follows –

Syntax

  1. Destructors will never have any arguments.
  2. The class name is used for the name of the destructor, with a ~ sign as a prefix to it.
class myClass
{
  public:
  // this automaticlly destroyed
  // the current instance of the class
  ~myClass();
};
Constructor and Destructors in C++ 2

Let us look at an example how we can implement destructors and constructors together in sync

Run

#include <iostream>
using namespace std;

class myClass
{
    public:
    myClass(){
        cout << "Constructor called" << endl;
    }
    
    ~myClass(){
        cout << "Destructor called" << endl;
    }
};

int main()
{
    myClass obj1; // Constructor Called for obj1
    
    int x = 1;
    
    if(x){
        myClass obj2; // Constructor Called for obj2
        
    } // Destructor Called for obj2 as obj2 out of scope
    
    return 0;
}// Destructor called for obj1 as obj1 out of scope

Output –

Constructor called
Constructor called
Destructor called
Destructor called

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription