Constructor Overloading in C++ - Multiple Constructors with Examples
Constructor overloading is an important feature of object-oriented programming in C++. It allows a class to have more than one constructor with different parameter lists. Using constructor overloading, we can initialize objects in different ways depending on the values supplied during object creation.
C++ में Constructor Overloading एक महत्वपूर्ण feature है। इसके द्वारा एक ही class में अलग-अलग parameters वाले एक से अधिक constructors define किए जा सकते हैं। इसकी सहायता से object बनाते समय आवश्यकता के अनुसार अलग-अलग तरीकों से initialization किया जा सकता है।
What is Constructor Overloading in C++?
Constructor overloading is the process of defining multiple constructors in the same class with different parameter lists. Each constructor can perform a different type of initialization depending on the arguments supplied when an object is created.
Definition: Constructor overloading in C++ is a feature that allows a class to define multiple constructors with different numbers or types of parameters.
परिभाषा: Constructor Overloading वह प्रक्रिया है जिसमें एक ही class में अलग-अलग संख्या या प्रकार के parameters वाले कई constructors define किए जाते हैं।
Why Do We Need Constructor Overloading?
Suppose we have a Student class containing roll number, name and marks.
Different objects may need different initialization methods:
One object may be created without providing any values.
Another object may be created using only a roll number.
Another object may be created using a roll number and marks.
Constructor overloading allows us to handle these situations using different constructors.
मान लीजिए Student class में roll number, name और marks हैं। अलग-अलग objects को अलग-अलग values के साथ initialize करने की आवश्यकता हो सकती है। Constructor overloading इस समस्या का समाधान करता है और object initialization को flexible बनाता है।
Syntax of Constructor Overloading
class ClassName{public:ClassName(){// Default constructor}ClassName(int a){// Constructor with one parameter}ClassName(int a, int b){// Constructor with two parameters}};
In this example, the class contains three constructors with different parameter lists.
इस उदाहरण में class के अंदर तीन constructors हैं। प्रत्येक constructor की parameter list अलग है।
Example 1: Constructor Overloading with Different Parameters
The following program demonstrates constructor overloading using three constructors.
#include <iostream>using namespace std;class Student{private:int rollNo;float marks;public:Student(){rollNo = 0;marks = 0;}Student(int r){rollNo = r;marks = 0;}Student(int r, float m){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, 85.5);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: 0Marks: 0Student 2:Roll Number: 101Marks: 0Student 3:Roll Number: 102Marks: 85.5
Explanation:
The Student class contains three overloaded constructors:
Student()initializes both data members with zero.Student(int r)initializes the roll number and sets marks to zero.Student(int r, float m)initializes both roll number and marks using supplied arguments.
When objects are created, C++ selects the appropriate constructor based on the arguments.
व्याख्या:
इस program में Student class के तीन constructors हैं:
Student()दोनों data members को zero से initialize करता है।Student(int r)केवल roll number initialize करता है।Student(int r, float m)roll number और marks दोनों initialize करता है।
Object बनाते समय दिए गए arguments के अनुसार उपयुक्त constructor अपने आप select होता है।
Example 2: Constructor Overloading with Different Data Types
Constructors can also be overloaded using different parameter data types.
अलग-अलग data types के parameters का उपयोग करके भी constructors को overload किया जा सकता है।
#include <iostream>#include <string>using namespace std;class Product{private:int productId;string productName;double price;public:Product(int id){productId = id;productName = "Unknown";price = 0;}Product(int id, string name){productId = id;productName = name;price = 0;}Product(int id, string name, double p){productId = id;productName = name;price = p;}void display(){cout << "Product ID: " << productId << endl;cout << "Product Name: " << productName << endl;cout << "Price: " << price << endl;}};int main(){Product p1(101);Product p2(102, "Keyboard");Product p3(103, "Laptop", 55000);cout << "Product 1:" << endl;p1.display();cout << "\nProduct 2:" << endl;p2.display();cout << "\nProduct 3:" << endl;p3.display();return 0;}
Output:
Product 1:Product ID: 101Product Name: UnknownPrice: 0Product 2:Product ID: 102Product Name: KeyboardPrice: 0Product 3:Product ID: 103Product Name: LaptopPrice: 55000
Explanation:
The first constructor accepts an integer.
The second constructor accepts an integer and a string.
The third constructor accepts an integer, a string and a double.
The compiler selects the appropriate constructor based on the supplied arguments.
इस उदाहरण में constructors की parameter lists अलग-अलग हैं। इसलिए object creation के समय दिए गए arguments के आधार पर compiler उपयुक्त constructor चुनता है।
Example 3: Constructor Overloading Using Member Initializer Lists
In modern C++, member initializer lists provide a convenient way to initialize data members.
Modern C++ में member initializer list की सहायता से data members को सीधे initialize किया जा सकता है।
#include <iostream>using namespace std;class Rectangle{private:int length;int breadth;public:Rectangle() : length(0), breadth(0){}Rectangle(int l) : length(l), breadth(l){}Rectangle(int l, int b) : length(l), breadth(b){}void displayArea(){cout << "Length: " << length << endl;cout << "Breadth: " << breadth << endl;cout << "Area: " << length * breadth << endl;}};int main(){Rectangle r1;Rectangle r2(5);Rectangle r3(10, 4);cout << "Rectangle 1:" << endl;r1.displayArea();cout << "\nRectangle 2:" << endl;r2.displayArea();cout << "\nRectangle 3:" << endl;r3.displayArea();return 0;}
Output:
Rectangle 1:Length: 0Breadth: 0Area: 0Rectangle 2:Length: 5Breadth: 5Area: 25Rectangle 3:Length: 10Breadth: 4Area: 40
Explanation:
The first constructor initializes both dimensions to zero.
The second constructor initializes both dimensions with the same value.
The third constructor accepts separate length and breadth values.
Each object is initialized according to the constructor selected.
इस program में पहला constructor zero values देता है, दूसरा square के लिए समान dimensions लेता है और तीसरा rectangle के लिए अलग-अलग dimensions स्वीकार करता है।
Example 4: Constructor Overloading with Copy Constructor
A class can also contain a copy constructor along with default and parameterized constructors.
एक class में default constructor, parameterized constructor और copy constructor तीनों एक साथ हो सकते हैं।
#include <iostream>using namespace std;class Number{private:int value;public:Number(){value = 0;}Number(int v){value = v;}Number(const Number &obj){value = obj.value;}void display(){cout << "Value: " << value << endl;}};int main(){Number n1;Number n2(100);Number n3(n2);cout << "Object 1:" << endl;n1.display();cout << "Object 2:" << endl;n2.display();cout << "Object 3:" << endl;n3.display();return 0;}
Output:
Object 1:Value: 0Object 2:Value: 100Object 3:Value: 100
Explanation:
Number()is the default constructor.Number(int v)is the parameterized constructor.Number(const Number &obj)is the copy constructor.All three constructors are used to initialize objects in different ways.
इस program में तीन अलग-अलग constructors का उपयोग किया गया है। प्रत्येक constructor object को अलग तरीके से initialize करता है।
Rules of Constructor Overloading in C++
- A class can have multiple constructors.एक class में एक से अधिक constructors हो सकते हैं।
- Constructors must have different parameter lists to be overloaded.Constructors की parameter lists अलग होनी चाहिए।
- Constructors can differ in the number of parameters.Parameters की संख्या अलग हो सकती है।
- Constructors can differ in parameter data types.Parameters के data types अलग हो सकते हैं।
- The compiler selects a suitable constructor based on the arguments and overload resolution rules.दिए गए arguments और overload resolution rules के आधार पर compiler उपयुक्त constructor चुनता है।
- Constructors cannot be overloaded using only different return types because constructors do not have return types.केवल return type बदलकर constructors को overload नहीं किया जा सकता।
- Default arguments can sometimes make constructor calls ambiguous if overloads are not designed carefully.Default arguments के कारण कुछ constructor calls ambiguous हो सकते हैं, इसलिए overloads सावधानी से define करने चाहिए।
Advantages of Constructor Overloading
- Flexible Initialization: Objects can be initialized in different ways.Objects को अलग-अलग तरीकों से initialize किया जा सकता है।
- Code Reusability: Multiple initialization options can be provided within the same class.एक ही class में initialization के कई विकल्प उपलब्ध होते हैं।
- Improved Readability: Object creation statements clearly indicate the values being supplied.Object creation statements से initialization values स्पष्ट होती हैं।
- Convenience: Programmers do not need separate initialization functions for every situation.प्रत्येक स्थिति के लिए अलग initialization functions बनाने की आवश्यकता कम होती है।
Constructor Overloading vs Function Overloading
| Constructor Overloading | Function Overloading |
|---|---|
| Multiple constructors are defined in a class. | Multiple functions with the same name are defined. |
| Constructor name is the same as the class name. | Functions can have any valid name. |
| Used mainly to initialize objects. | Used to perform operations in different ways. |
| Has no return type. | Functions may have return types. |
| Invoked during object creation or initialization. | Generally invoked through function calls. |
Frequently Asked Questions (FAQs)
1. What is constructor overloading in C++?
Constructor overloading is the process of defining multiple constructors in the same class with different parameter lists.
2. Can a class have multiple constructors?
Yes. A class can have multiple constructors, provided their parameter lists allow them to be distinguished through overload resolution.
3. Can constructors be overloaded by changing the number of parameters?
Yes. Constructors can be overloaded by using different numbers of parameters.
4. Can constructors be overloaded by changing parameter data types?
Yes. Constructors can have different parameter data types.
5. Can we overload a default constructor?
A class can have a no-argument constructor along with other constructors. However, there can be only one constructor with an identical parameter list.
6. Can a constructor be overloaded only by changing its return type?
No. Constructors have no return type, and overloading depends on their parameter lists.
7. Can a copy constructor be part of constructor overloading?
Yes. A copy constructor can be defined along with other constructors in the same class.
Constructor overloading allows a class to provide multiple ways to initialize its objects. By defining constructors with different parameter lists, programmers can create objects with different initial values according to their requirements. It is an important concept in C++ and is widely used in object-oriented programming.
Comments
Post a Comment