Default Constructor in C++ with Examples

A default constructor in C++ is a constructor that can be called without providing any arguments. It is used to initialize an object when no constructor arguments are supplied. A default constructor may be provided by the compiler or defined by the programmer.

C++ में Default Constructor ऐसा constructor होता है जिसे बिना arguments दिए कॉल किया जा सकता है। इसका उपयोग object बनाते समय उसे initialize करने के लिए किया जाता है।

What is a Default Constructor in C++?

A default constructor is a constructor that can be invoked without passing any arguments.

For example:

Student s1;

Here, s1 is an object of the Student class. If a suitable default constructor is available, it is called automatically.

यहाँ Student s1; से Student क्लास का object बनाया गया है। यदि क्लास में उपयुक्त default constructor उपलब्ध है, तो वह अपने आप कॉल होता है।

Syntax of Default Constructor

class ClassName
{
public:
ClassName()
{
// Initialization statements
}
};

The constructor has the same name as the class and does not have a return type.

कंस्ट्रक्टर का नाम क्लास के नाम के समान होता है और इसका कोई return type नहीं होता है।

Types of Default Constructors

Default constructors are commonly discussed in two categories:

  1. Compiler-generated default constructor

  2. User-defined default constructor

1. Compiler-Generated Default Constructor

If a class does not declare any constructor, the compiler implicitly declares a default constructor for that class.

This compiler-generated constructor does not automatically initialize ordinary built-in data members to zero. Their values may remain indeterminate when the object is default-initialized.

यदि किसी क्लास में कोई constructor declare नहीं किया गया है, तो compiler सामान्यतः उसके लिए implicit default constructor उपलब्ध कराता है।

लेकिन यह constructor सामान्य built-in data members को अपने आप zero initialize नहीं करता।

Example:

#include <iostream>
using namespace std;
class Demo
{
public:
int number;
};
int main()
{
Demo d1;
d1.number = 100;
cout << "Number = " << d1.number << endl;
return 0;
}

Output:

Number = 100

Explanation:

  • The Demo class does not declare any constructor.

  • The compiler provides an implicit default constructor.

  • The object d1 is created using Demo d1;.

  • We explicitly assign 100 to number before displaying it.

Note: Do not read an uninitialized built-in data member. Its value is not automatically guaranteed to be zero.

2. User-Defined Default Constructor

A default constructor written by the programmer is called a user-defined default constructor.

The programmer can use it to initialize data members with suitable values.

प्रोग्रामर द्वारा स्वयं बनाया गया default constructor, User-Defined Default Constructor कहलाता है। इसका उपयोग data members को निर्धारित प्रारंभिक मान देने के लिए किया जाता है।

Example:

#include <iostream>
using namespace std;
class Student
{
private:
int rollNo;
float marks;
public:
Student()
{
rollNo = 101;
marks = 90.5;
}
void display()
{
cout << "Roll Number: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
};
int main()
{
Student s1;
s1.display();
return 0;
}

Output:

Roll Number: 101
Marks: 90.5

Explanation:

  1. The Student class contains two private data members.

  2. The user-defined constructor Student() initializes rollNo and marks.

  3. When Student s1; is executed, the constructor is automatically called.

  4. The display() function displays the initialized values.

इस प्रोग्राम में Student() constructor, rollNo को 101 और marks को 90.5 से initialize करता है। जैसे ही s1 object बनाया जाता है, constructor अपने आप कॉल होता है।

Example 2: Default Constructor with Multiple Objects

A default constructor is called separately for every object created.

एक ही क्लास के कई objects बनाने पर प्रत्येक object के लिए default constructor अलग-अलग कॉल होता है।

#include <iostream>
using namespace std;
class Demo
{
private:
int value;
public:
Demo()
{
value = 50;
cout << "Default constructor called" << endl;
}
void display()
{
cout << "Value = " << value << endl;
}
};
int main()
{
Demo d1;
Demo d2;
Demo d3;
d1.display();
d2.display();
d3.display();
return 0;
}

Output:

Default constructor called
Default constructor called
Default constructor called
Value = 50
Value = 50
Value = 50

Explanation:

Three objects are created: d1, d2 and d3. Therefore, the default constructor is called three times, once for each object.

इस प्रोग्राम में तीन objects बनाए गए हैं। इसलिए default constructor तीन बार कॉल होता है और प्रत्येक object का value 50 से initialize होता है।

Example 3: Default Constructor Using Member Initializer List

In modern C++, a member initializer list is a preferred way to initialize data members.

Modern C++ में data members को initialize करने के लिए member initializer list का उपयोग करना एक अच्छा तरीका है।

#include <iostream>
using namespace std;
class Employee
{
private:
int id;
double salary;
public:
Employee() : id(101), salary(25000.0)
{
}
void display()
{
cout << "Employee ID: " << id << endl;
cout << "Salary: " << salary << endl;
}
};
int main()
{
Employee e1;
e1.display();
return 0;
}

Output:

Employee ID: 101
Salary: 25000

Explanation:

The expression:

Employee() : id(101), salary(25000.0)

is called a member initializer list. It initializes the data members before the constructor body executes.

यहाँ id और salary को constructor body के अंदर assignment करने के बजाय member initializer list द्वारा initialize किया गया है।

Characteristics of Default Constructor

  1. A default constructor can be called without arguments.
    Default constructor को बिना arguments के कॉल किया जा सकता है।

  2. It has the same name as the class.
    इसका नाम क्लास के नाम के समान होता है।

  3. It does not have a return type.
    इसका कोई return type नहीं होता है।

  4. It is automatically invoked when an object is created without constructor arguments.
    बिना constructor arguments के object बनाने पर यह अपने आप कॉल होता है।

  5. It can be defined by the programmer or implicitly provided by the compiler.
    इसे प्रोग्रामर define कर सकता है या compiler implicit रूप से उपलब्ध करा सकता है।

  6. It can initialize data members using assignments or a member initializer list.
    यह assignments या member initializer list द्वारा data members को initialize कर सकता है।

  7. If a class declares a constructor with parameters but does not provide a default constructor, the compiler does not automatically generate a default constructor for that class.
    यदि क्लास में parameterized constructor declare किया गया है और default constructor नहीं बनाया गया है, तो compiler अपने आप default constructor generate नहीं करता।

Important Difference: Default Constructor vs Constructor with Default Arguments

A constructor with default arguments can also be called without passing arguments. Therefore, it can serve as a default constructor.

उदाहरण के लिए:

#include <iostream>
using namespace std;
class Demo
{
public:
Demo(int x = 10)
{
cout << "Value = " << x << endl;
}
};
int main()
{
Demo d1;
Demo d2(50);
return 0;
}

Output:

Value = 10
Value = 50

Here, Demo(int x = 10) is not a default constructor in the strict technical sense because it has a parameter. However, it is a constructor that can be called with no arguments, so it provides default-construction capability.

यहाँ constructor में एक parameter है, लेकिन उसे default argument दिया गया है। इसलिए इसे बिना argument के भी कॉल किया जा सकता है।

Default Constructor vs Parameterized Constructor

Default ConstructorParameterized Constructor
Can be called without arguments.Accepts one or more arguments when called in its parameterized form.
Commonly initializes objects with predefined values.Initializes objects using supplied values.
Example: Student()Example: Student(int r)
Example object: Student s1;Example object: Student s1(101);

Frequently Asked Questions (FAQs)

1. What is a default constructor in C++?

A default constructor is a constructor that can be called without providing arguments.

2. Does the compiler create a default constructor automatically?

The compiler implicitly declares a default constructor if the class does not declare any constructor. If the class declares another constructor, an implicit default constructor is not provided.

3. Does a default constructor initialize data members to zero?

Not necessarily. A compiler-generated default constructor does not guarantee zero initialization of ordinary built-in data members. A programmer-defined constructor can explicitly initialize them.

4. Can a default constructor accept parameters?

A constructor with parameters having default values can be called without arguments. However, a default constructor in the strict technical sense has no parameters.

5. Can we overload a default constructor?

Yes. A class can have multiple constructors with different parameter lists, including a no-argument constructor.

6. What happens if we define only a parameterized constructor?

The compiler does not implicitly provide a default constructor. If you want to create an object without arguments, you must provide a suitable default constructor or a constructor callable without arguments.

A default constructor is an important feature of C++ that allows objects to be created without passing initialization arguments. It can be implicitly provided by the compiler or explicitly defined by the programmer. Understanding default constructors is essential before learning parameterized constructors, constructor overloading and copy constructors.

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)