Parameterized Constructor in C++ with Examples
A parameterized constructor in C++ is a constructor that accepts one or more arguments. It is used to initialize the data members of an object with values supplied when the object is created. Parameterized constructors allow us to create multiple objects of the same class with different initial values.
C++ में Parameterized Constructor ऐसा constructor होता है जो एक या एक से अधिक arguments स्वीकार करता है। इसका उपयोग object बनाते समय उसके data members को दिए गए values से initialize करने के लिए किया जाता है।
What is a Parameterized Constructor in C++?
A parameterized constructor is a special member function of a class that has parameters. These parameters receive values when an object is created.
Definition: A parameterized constructor is a constructor that accepts arguments to initialize the data members of an object.
परिभाषा: Parameterized Constructor एक ऐसा constructor है जो object के data members को initialize करने के लिए arguments स्वीकार करता है।
For example:
Student s1(101, 85.5);Here, 101 and 85.5 are arguments passed to the constructor while creating the object.
यहाँ object बनाते समय constructor को 101 और 85.5 arguments के रूप में दिए गए हैं।
Syntax of Parameterized Constructor
class ClassName{public:ClassName(data_type argument1, data_type argument2){// Initialization statements}};
The constructor receives values through its parameters and uses them to initialize the data members.
कंस्ट्रक्टर अपने parameters के माध्यम से values प्राप्त करता है और उनका उपयोग data members को initialize करने के लिए करता है।
Example 1: Simple Parameterized Constructor
The following program demonstrates a parameterized constructor that initializes the data members of a class.
#include <iostream>using namespace std;class Student{private:int rollNo;float marks;public:Student(int r, float m){rollNo = r;marks = m;}void display(){cout << "Roll Number: " << rollNo << endl;cout << "Marks: " << marks << endl;}};int main(){Student s1(101, 85.5);s1.display();return 0;}
Output:
Roll Number: 101Marks: 85.5
Explanation:
The
Studentclass contains two private data members:rollNoandmarks.The constructor
Student(int r, float m)accepts two parameters.The statement
Student s1(101, 85.5);creates an object and passes two arguments.The constructor assigns these values to the data members.
The
display()function displays the initialized values.
व्याख्या:
Studentक्लास में दो private data members हैं:rollNoऔरmarks।Student(int r, float m)constructor दो parameters स्वीकार करता है।Student s1(101, 85.5);object बनाते समय दो arguments pass करता है।Constructor इन values को data members में assign करता है।
display()function इन values को प्रदर्शित करता है।
Example 2: Parameterized Constructor with Multiple Objects
A parameterized constructor allows us to initialize different objects with different values.
Parameterized constructor की सहायता से एक ही क्लास के अलग-अलग objects को अलग-अलग values से initialize किया जा सकता है।
#include <iostream>using namespace std;class Rectangle{private:int length;int breadth;public:Rectangle(int l, int b){length = l;breadth = b;}void calculateArea(){cout << "Area = " << length * breadth << endl;}};int main(){Rectangle r1(10, 5);Rectangle r2(8, 4);cout << "Rectangle 1: ";r1.calculateArea();cout << "Rectangle 2: ";r2.calculateArea();return 0;}
Output:
Rectangle 1: Area = 50Rectangle 2: Area = 32
Explanation:
The
Rectangleconstructor accepts two arguments: length and breadth.Object
r1is initialized with values10and5.Object
r2is initialized with values8and4.The
calculateArea()function calculates and displays the area of each rectangle.
इस प्रोग्राम में r1 और r2 objects को अलग-अलग dimensions दिए गए हैं। Constructor प्रत्येक object के लिए उसकी length और breadth initialize करता है।
Example 3: Parameterized Constructor Using Member Initializer List
In modern C++, member initializer lists are commonly preferred for initializing data members.
Modern C++ में data members को initialize करने के लिए member initializer list का उपयोग करना एक अच्छा तरीका है।
#include <iostream>#include <string>using namespace std;class Employee{private:int id;string name;double salary;public:Employee(int i, string n, double s): id(i), name(n), salary(s){}void display(){cout << "Employee ID: " << id << endl;cout << "Name: " << name << endl;cout << "Salary: " << salary << endl;}};int main(){Employee e1(101, "Amit", 35000);e1.display();return 0;}
Output:
Employee ID: 101Name: AmitSalary: 35000
Explanation:
The following part is called a member initializer list:
Employee(int i, string n, double s): id(i), name(n), salary(s){}
It initializes the data members before the constructor body executes.
यहाँ id, name और salary को constructor body के अंदर assign करने के बजाय member initializer list द्वारा initialize किया गया है।
Example 4: Parameterized Constructor with Different Data Types
A parameterized constructor can accept parameters of different data types.
Parameterized constructor अलग-अलग data types के parameters भी स्वीकार कर सकता है।
#include <iostream>#include <string>using namespace std;class Product{private:int productId;string productName;double price;public: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(501, "Laptop", 55000.50);p1.display();return 0;}
Output:
Product ID: 501Product Name: LaptopPrice: 55000.5
Explanation:
The constructor accepts an integer, a string and a double value. These arguments initialize the corresponding data members.
इस उदाहरण में constructor integer, string और double प्रकार के arguments स्वीकार करता है और संबंधित data members को initialize करता है।
Characteristics of Parameterized Constructor
- A parameterized constructor accepts one or more arguments.यह एक या अधिक arguments स्वीकार करता है।
- Its name is the same as the class name.इसका नाम क्लास के नाम के समान होता है।
- It does not have a return type.इसका कोई return type नहीं होता है।
- It is automatically called when an object is created with matching arguments.उपयुक्त arguments के साथ object बनाने पर यह अपने आप कॉल होता है।
- It is used to initialize objects with user-supplied values.इसका उपयोग user द्वारा दिए गए values से objects को initialize करने के लिए किया जाता है।
- A class can have multiple parameterized constructors through constructor overloading.Constructor overloading के माध्यम से एक क्लास में कई parameterized constructors हो सकते हैं।
- It can use a member initializer list to initialize data members.Data members को initialize करने के लिए इसमें member initializer list का उपयोग किया जा सकता है।
Advantages of Parameterized Constructor
- Different Initial Values: Different objects can be initialized with different values.अलग-अलग objects को अलग-अलग प्रारंभिक values दी जा सकती हैं।
- Automatic Initialization: Data members are initialized during object creation.Object बनते समय data members initialize हो जाते हैं।
- Less Repetitive Code: Separate initialization statements after object creation can often be avoided.Object बनाने के बाद अलग-अलग initialization statements लिखने की आवश्यकता कम हो जाती है।
- Better Object Initialization: Objects can be created with the values required by the program.प्रोग्राम की आवश्यकता के अनुसार objects बनाए जा सकते हैं।
Parameterized Constructor vs Default Constructor
| Parameterized Constructor | Default Constructor |
|---|---|
| Accepts one or more parameters in its parameterized form. | Can be called without arguments. |
| Used to initialize objects with supplied values. | Often used to initialize objects with predefined or default values. |
Example: Student(int r) | Example: Student() |
Example call: Student s1(101); | Example call: Student s1; |
What Happens If We Define Only a Parameterized Constructor?
If a class declares a parameterized constructor but does not declare a default constructor, the compiler does not automatically provide an implicit default constructor.
यदि किसी क्लास में केवल parameterized constructor define किया गया है, तो compiler अपने आप default constructor उपलब्ध नहीं कराता।
Example:
#include <iostream>using namespace std;class Student{private:int rollNo;public:Student(int r){rollNo = r;}void display(){cout << "Roll Number: " << rollNo << endl;}};int main(){Student s1(101);s1.display();return 0;}
Output:
Roll Number: 101Here, Student s1(101); is valid because it passes an argument to the parameterized constructor.
However, Student s2; would produce a compilation error because the class has no constructor that can be called without arguments.
यदि हम Student s2; लिखेंगे, तो compilation error आएगा क्योंकि इस क्लास में बिना arguments के कॉल होने वाला constructor उपलब्ध नहीं है।
Frequently Asked Questions (FAQs)
1. What is a parameterized constructor in C++?
A parameterized constructor is a constructor that accepts arguments to initialize the data members of an object.
2. Can a parameterized constructor have multiple parameters?
Yes. A parameterized constructor can accept multiple parameters of the same or different data types.
3. Can we create multiple objects using a parameterized constructor?
Yes. We can create multiple objects and initialize each object with different arguments.
4. Can a class have both default and parameterized constructors?
Yes. A class can have both types of constructors. This is commonly achieved through constructor overloading.
5. Is a parameterized constructor called automatically?
Yes. It is automatically invoked when an object is created with arguments matching the constructor.
6. Can a parameterized constructor use default arguments?
Yes. A constructor can have default arguments, allowing it to be called with fewer arguments or even without arguments.
A parameterized constructor is useful when objects need to be initialized with different values during their creation. It makes object initialization convenient and reduces repetitive code. Parameterized constructors are also an important foundation for understanding constructor overloading, copy constructors and constructors with default arguments in C++.
Comments
Post a Comment