Constructor with Default Arguments in C++ with Examples

A constructor with default arguments in C++ is a constructor in which one or more parameters are assigned default values. These values are automatically used when the corresponding arguments are not provided during object creation. C++ allows us to define default arguments in constructors just like ordinary functions. This makes object initialization more flexible and reduces the need to write multiple constructors.

C++ में Default Arguments वाला Constructor ऐसा constructor होता है जिसके एक या अधिक parameters को पहले से default values दी जाती हैं। जब object बनाते समय संबंधित arguments नहीं दिए जाते, तो constructor उन default values का उपयोग करता है।

1. Syntax of Constructor with Default Arguments

The general syntax is:

class ClassName {
public:
ClassName(dataType parameter1 = value1,
dataType parameter2 = value2) {
// Constructor body
}
};

In this syntax:

  • ClassName is the name of the constructor.

  • Parameters are assigned default values.

  • If an argument is not supplied during object creation, its default value is used.

इस syntax में constructor के parameters को default values दी जाती हैं। Object बनाते समय यदि कोई argument नहीं दिया जाता, तो उसकी default value अपने आप उपयोग हो जाती है।

2. Example of Constructor with Default Arguments in C++

The following program demonstrates a constructor with default arguments.

C++ Program

#include <iostream>
using namespace std;
class Student {
int rollNo;
float marks;
public:
Student(int r = 0, float m = 0.0f) {
rollNo = r;
marks = m;
}
void display() {
cout << "Roll Number: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
};
int main() {
Student s1;
Student s2(101);
Student s3(102, 88.5f);
cout << "Student 1:" << endl;
s1.display();
cout << "\nStudent 2:" << endl;
s2.display();
cout << "\nStudent 3:" << endl;
s3.display();
return 0;
}

Output

Student 1:
Roll Number: 0
Marks: 0
Student 2:
Roll Number: 101
Marks: 0
Student 3:
Roll Number: 102
Marks: 88.5

Explanation

In this program, the constructor is defined as:

Student(int r = 0, float m = 0.0f)

It has two parameters with default values:

  • r = 0

  • m = 0.0f

Three objects are created in main():

1. Student s1;

No arguments are supplied. Therefore, both default values are used.

  • Roll number = 0

  • Marks = 0.0

2. Student s2(101);

Only one argument is supplied. The first parameter receives 101, while the second parameter uses its default value 0.0.

3. Student s3(102, 88.5f);

Both arguments are supplied, so the default values are not used.

इस उदाहरण में एक ही constructor तीन अलग-अलग तरीकों से objects को initialize कर रहा है। इससे हमें अलग-अलग object initialization के लिए कई constructors लिखने की आवश्यकता नहीं पड़ती।

3. Example of Constructor with Partial Default Arguments

Default arguments can also be provided to only some parameters. However, parameters with default values must appear after all parameters without default values.

केवल कुछ parameters को भी default values दी जा सकती हैं। लेकिन default value वाले parameters को उन parameters के बाद लिखना आवश्यक है जिनकी default values नहीं हैं।

C++ Program

#include <iostream>
using namespace std;
class Rectangle {
int length;
int breadth;
public:
Rectangle(int l, int b = 1) {
length = l;
breadth = b;
}
void calculateArea() {
cout << "Length: " << length << endl;
cout << "Breadth: " << breadth << endl;
cout << "Area: " << length * breadth << endl;
}
};
int main() {
Rectangle r1(10);
Rectangle r2(10, 5);
cout << "Rectangle 1:" << endl;
r1.calculateArea();
cout << "\nRectangle 2:" << endl;
r2.calculateArea();
return 0;
}

Output

Rectangle 1:
Length: 10
Breadth: 1
Area: 10
Rectangle 2:
Length: 10
Breadth: 5
Area: 50

Explanation

The constructor is:

Rectangle(int l, int b = 1)

Here, l is a required parameter, while b has a default value of 1.

  • Rectangle r1(10); uses 10 for length and the default value 1 for breadth.

  • Rectangle r2(10, 5); uses the supplied values 10 and 5.

इस उदाहरण में length देना अनिवार्य है, जबकि breadth नहीं देने पर उसका default value 1 उपयोग किया जाता है।

4. Rules for Default Arguments in Constructors

The following rules should be considered while using default arguments in constructors:

  1. Default values are assigned in the constructor parameter list.

  2. Parameters with default values must appear after parameters without default values.

  3. Arguments supplied during object creation replace the corresponding default values.

  4. A constructor with default arguments can be called with fewer arguments.

  5. If all constructor parameters have default values, the constructor can be called without passing any arguments.

  6. Avoid defining constructor overloads that make a call ambiguous.

C++ में default arguments का उपयोग करते समय इन नियमों का ध्यान रखना चाहिए। विशेष रूप से parameter order और constructor overloading के कारण होने वाली ambiguity से बचना चाहिए।

5. Constructor with Default Arguments vs Constructor Overloading

Both techniques allow objects to be initialized in different ways, but they work differently.

Constructor with Default ArgumentsConstructor Overloading
Default values are assigned to parameters.Multiple constructors are defined.
One constructor can handle different argument counts.Different constructors handle different argument lists.
Usually requires less code.May require more code.
Can cause ambiguity when combined carelessly with overloaded constructors.Requires distinguishable parameter lists and unambiguous overload resolution.

Example of a constructor with default arguments:

Student(int r = 0, float m = 0.0f);

Example of constructor overloading:

Student() {
// Default initialization
}
Student(int r) {
// Initialize roll number
}
Student(int r, float m) {
// Initialize roll number and marks
}

Default arguments provide flexibility through one constructor, whereas constructor overloading provides multiple constructor definitions.

Default Arguments में एक constructor के parameters को default values दी जाती हैं, जबकि Constructor Overloading में अलग-अलग parameter lists के साथ कई constructors define किए जाते हैं।

6. Ambiguity in Constructors with Default Arguments

Care must be taken when combining default arguments with constructor overloading.

For example, the following class can create ambiguity:

class Student {
public:
Student() {
}
Student(int r = 0) {
}
};

Now consider:

Student s1;

Both constructors can be called without arguments. Therefore, the compiler cannot select one unambiguously, and the program is ill-formed.

To avoid this problem, do not define overlapping constructors that can accept the same argument list.

Constructor Overloading और Default Arguments को एक साथ उपयोग करते समय ध्यान रखें कि एक ही object creation statement के लिए एक से अधिक constructors eligible न हों।

7. Advantages of Constructor with Default Arguments

  • Reduces code duplication: One constructor can handle multiple initialization cases.

  • Flexible initialization: Objects can be created with different numbers of arguments.

  • Simpler class design: Fewer constructor definitions may be required.

  • Improved readability: Default values make common initialization choices clear.

इसके मुख्य लाभ हैं कि code duplication कम होता है, object initialization अधिक flexible बनता है और class design सरल हो सकता है।

8. Frequently Asked Questions (FAQs)

Q1. What is a constructor with default arguments in C++?

A constructor with default arguments is a constructor whose parameters have predefined values. These values are used when corresponding arguments are not supplied during object creation.

C++ में ऐसा constructor जिसके parameters को पहले से default values दी जाती हैं, Constructor with Default Arguments कहलाता है।

Q2. Can a constructor have default arguments in C++?

Yes. C++ allows constructors to have default arguments, just like ordinary functions.

हाँ, C++ में constructors के parameters को default arguments दिए जा सकते हैं।

Q3. Can a constructor with default arguments be called without arguments?

Yes, if all its parameters have default values.

हाँ, यदि constructor के सभी parameters की default values हैं, तो उसे बिना arguments के call किया जा सकता है।

Q4. What is the difference between default constructor and constructor with default arguments?

A default constructor is a constructor that can be called without arguments. It may have no parameters or have parameters for which all arguments have default values. A constructor such as Student(int r = 0) is therefore also a default constructor in the C++ language terminology, even though textbooks often describe it as a constructor with default arguments.

Q5. Can we use constructor overloading and default arguments together?

Yes, but the overloads must be designed carefully. If more than one constructor can accept the same call, the compiler reports an ambiguity.

हाँ, दोनों का उपयोग एक साथ किया जा सकता है, लेकिन constructor calls ambiguous नहीं होने चाहिए।

A constructor with default arguments is a useful feature of C++ that allows objects to be initialized with different numbers of arguments using a single constructor. It reduces code duplication and makes object initialization flexible.

C++ में Default Arguments वाले Constructors का उपयोग object initialization को सरल और flexible बनाने के लिए किया जाता है। एक ही constructor अलग-अलग argument combinations के साथ objects को initialize कर सकता है।

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)