Default Constructor in C++

 


A default constructor in C++ is a constructor that takes no parameters or has default values for all parameters. It is automatically called when an object of a class is created.


1️⃣ Characteristics of a Default Constructor

✅ It initializes objects when no arguments are provided.
✅ It has the same name as the class and no return type.
✅ It is automatically invoked when an object is created.
✅ If no constructor is defined, C++ provides a default constructor implicitly.


2️⃣ Example: Default Constructor

  • #include <iostream>

  • using namespace std;


  • class Car {

  • public:

  •     string brand;

  •     

  •     // Default Constructor

  •     Car() {

  •         brand = "Unknown";

  •         cout << "Default Constructor Called!" << endl;

  •     }


  •     void display() {

  •         cout << "Car Brand: " << brand << endl;

  •     }

  • };


  • int main() {

  •     Car myCar;  // Default constructor is automatically called

  •     myCar.display();

  •     return 0;

  • }


Output:

  • Default Constructor Called!

  • Car Brand: Unknown


Explanation:

  • The constructor Car() initializes brand = "Unknown".

  • When Car myCar; is created, the default constructor is called automatically.


3️⃣ Example: Default Constructor with Initialization

  • #include <iostream>

  • using namespace std;


  • class Student {

  • private:

  •     string name;

  •     int age;


  • public:

  •     // Default Constructor

  •     Student() {

  •         name = "John Doe";

  •         age = 18;

  •     }


  •     void display() {

  •         cout << "Name: " << name << ", Age: " << age << endl;

  •     }

  • };


  • int main() {

  •     Student student1;  // Default constructor is called

  •     student1.display();

  •     return 0;

  • }


Output:

  • Name: John Doe, Age: 18


Explanation:

  • The constructor initializes name = "John Doe" and age = 18.

  • When an object is created, it automatically gets these default values.


4️⃣ What Happens If No Constructor is Defined?

If you do not define a constructor, C++ automatically provides an empty default constructor.

Example: Implicit Default Constructor

  • #include <iostream>

  • using namespace std;


  • class Example {

  • public:

  •     void show() {

  •         cout << "No Constructor Defined, But Object Created!" << endl;

  •     }

  • };


  • int main() {

  •     Example obj;  // Implicit default constructor is provided by C++

  •     obj.show();

  •     return 0;

  • }


Output:

  • No Constructor Defined, But Object Created!


Explanation:

  • Since no constructor is defined, C++ provides an empty constructor automatically.


5️⃣ Default Constructor with Dynamic Memory Allocation

  • #include <iostream>

  • using namespace std;


  • class Array {

  • private:

  •     int *arr;

  •     int size;


  • public:

  •     // Default Constructor

  •     Array() {

  •         size = 5;

  •         arr = new int[size]{1, 2, 3, 4, 5};  // Initialize with default values

  •     }


  •     void display() {

  •         for (int i = 0; i < size; i++)

  •             cout << arr[i] << " ";

  •         cout << endl;

  •     }


  •     ~Array() {  // Destructor to free memory

  •         delete[] arr;

  •     }

  • };


  • int main() {

  •     Array obj;

  •     obj.display();

  •     return 0;

  • }


Output:

  • 1 2 3 4 5


Explanation:

  • The constructor initializes an array dynamically.

  • Memory is freed in the destructor (~Array()).


6️⃣ When to Use Default Constructor?

✅ When an object requires default initialization.
✅ When you want to set default values to class members.
✅ When using dynamic memory allocation to initialize objects.

Would you like to see Parameterized Constructors next? 🚀


Post a Comment

0 Comments