Constructors In C++

In C++, constructors are special member functions that are automatically called when an object of a class is created. They are used to initialize the object's data members. They have the same name as the class. Constructors can be classified into several types, including default constructors, parameterized constructors. For example,

class MyClass {
public:
    // create a constructor
    MyClass() {
    }
};

Default Constructor

A default constructor is a constructor that doesn't take any arguments. It initializes the object's data members to default values or performs any necessary setup. If you don't provide a constructor explicitly, the compiler generates a default constructor for you.

#include <iostream>
using namespace std;
 
class MyClass {
public:
    // Default Constructor
    MyClass() {
        cout << "Default constructor called" << endl;
    }
};
 
int main() {
  MyClass obj;
  return 0;
}

Output:

Default constructor called

Here,

  • The default constructor is called when an object of the MyClass class is created using the statement MyClass obj; in the main function.

Parameterized Constructor

A parameterized constructor is a constructor that accepts parameters to initialize the object's data members. It allows you to provide initial values to the object during its creation.

#include <iostream>
using namespace std;
 
class MyClass {
public:
 
    int num1;
    float num2;
    
    // Parameterized Constructor
    MyClass(int x, float y) {
        cout << "Parameterized constructor called" << endl;
        num1 = x; // Initialize member num1
        num2 = y; // Initialize member num2
    }
 
    void display() {
        cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
    }
};
 
int main() {
 
    // Parameterized Constructor
    MyClass obj(7, 5.7);
    obj.display();
 
    return 0;
}

Output:

Parameterized constructor called
num1 = 7, num2 = 5.7

Here,

  • The parameterized constructor is called when an object of the MyClass class is created with the values 7 and 5.7.
  • The display function is called on the obj object to print its data members.

Constructors with Default Arguments

Constructors can also have default arguments, allowing you to create objects without providing values for all parameters. Default arguments are specified in the constructor declaration. Here's an example:

#include <iostream>
using namespace std;
 
class MyClass {
public:
    int num1;
    float num2;
    
    // Constructor with default arguments
    MyClass(int x = 0, float y = 0.0) {
        num1 = x; // Initialize member num1
        num2 = y; // Initialize member num2
    }
 
    void display() {
        cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
    }
 
};
 
int main() {
    // Create objects using the constructor with default arguments
    MyClass obj1;         // Uses default values (0 and 0.0)
    MyClass obj2(10);     // Uses default value for num2 (0.0)
    MyClass obj3(10, 5.5); // Uses provided values
 
    // Display the values of objects
    obj1.display();
    obj2.display();
    obj3.display();
 
    return 0;
}

Output:

num1 = 0, num2 = 0
num1 = 10, num2 = 0
num1 = 10, num2 = 5.5

In this example:

  • We have defined a class MyClass with a constructor that takes two parameters, x and y, with default values 0 and 0.0, respectively.
  • In the main function, we create three objects of type MyClass using different ways:
    • obj1: Created without any arguments, so default values are used.
    • obj2: Created with only one argument, so the default value for num2 is used.
    • obj3: Created with both arguments provided, so the provided values are used.
  • We then display the values of each object using the display function.

Destructor In C++

In C++, a destructor is a special member function that is automatically called when an object goes out of scope or is explicitly deleted. Its primary purpose is to release any resources allocated by the object after it is destroyed. Destructors have the same name as the class preceded by a tilde (~).

For Example:

#include <iostream>
using namespace std;
 
class MyClass {
public:
    // Constructor
    MyClass() {
        cout << "Constructor called" << endl;
    }
 
    // Destructor
    ~MyClass() {
        cout << "Destructor called" << endl;
    }
};
 
int main() {
    MyClass obj; // Creating an object of MyClass
    // obj goes out of scope here, so the destructor is called automatically
    return 0;
}

Output:

Constructor called
Destructor called

In this example:

  • The MyClass class has a constructor that prints "Constructor called" when an object is created.
  • The destructor of MyClass prints "Destructor called" when the object is destroyed, which happens when the object goes out of scope at the end of the main() function.

Difference Between Constructor And Destructor

ConstructorDestructor
Automatically called when an object is createdAutomatically called when an object is destroyed
Helps to initialize the object of a classUsed to deallocate resources
There can be multiple constructors in a classThere is always a single destructor in a class
May accept argumentsDoes not accept arguments
Can be overloadedCan't be overloaded
How's article quality?

Last updated on -

Page Contents