Stream Insertion and Extraction Operator Overloading in C++ (<< and >>)
In C++, the stream insertion (<<) and stream extraction (>>) operators are commonly used for displaying and accepting data. The insertion operator is used with cout to display output, whereas the extraction operator is used with cin to accept input. We can overload these operators to work directly with objects of user-defined classes.
C++ में Stream Insertion (<<) और Stream Extraction (>>) operators का उपयोग data को display करने और input लेने के लिए किया जाता है। Insertion operator का उपयोग cout के साथ output दिखाने के लिए किया जाता है, जबकि Extraction operator का उपयोग cin के साथ input लेने के लिए किया जाता है। हम इन operators को overload करके user-defined classes के objects के साथ सीधे उपयोग कर सकते हैं।
What is Stream Insertion Operator Overloading?
The stream insertion operator (<<) is used to send data to an output stream. In C++, it is commonly used with cout.
Stream Insertion Operator (<<) का उपयोग output stream में data भेजने के लिए किया जाता है। C++ में इसका सामान्य उपयोग cout के साथ होता है।
For example:
उदाहरण:
cout << "Hello World";When we overload the << operator, we can directly display the data members of an object using cout.
जब हम << operator को overload करते हैं, तो हम cout का उपयोग करके किसी object के data members को सीधे display कर सकते हैं।
For example:
उदाहरण:
cout << obj;Here, obj is an object of a user-defined class.
यहाँ obj किसी user-defined class का object है।
What is Stream Extraction Operator Overloading?
The stream extraction operator (>>) is used to receive data from an input stream. In C++, it is commonly used with cin.
Stream Extraction Operator (>>) का उपयोग input stream से data प्राप्त करने के लिए किया जाता है। C++ में इसका सामान्य उपयोग cin के साथ होता है।
For example:
उदाहरण:
cin >> number;When we overload the >> operator, we can directly accept input into the data members of an object using cin.
जब हम >> operator को overload करते हैं, तो हम cin की सहायता से किसी object के data members में सीधे input प्राप्त कर सकते हैं।
For example:
उदाहरण:
cin >> obj;Here, obj is an object of a user-defined class.
यहाँ obj किसी user-defined class का object है।
Why Are Stream Operators Overloaded Using Friend Functions?
The stream insertion and extraction operators are generally overloaded as non-member functions, often declared as friend functions inside the class.
Stream Insertion और Extraction operators को सामान्यतः non-member functions के रूप में overload किया जाता है, जिन्हें अक्सर class के अंदर friend functions के रूप में declare किया जाता है।
The reason is that the left operand of the insertion operator is an output stream such as cout, and the left operand of the extraction operator is an input stream such as cin.
इसका कारण यह है कि Insertion operator का left operand cout जैसी output stream होती है और Extraction operator का left operand cin जैसी input stream होती है।
For example:
उदाहरण:
cout << obj;cin >> obj;
If these operators were overloaded as ordinary member functions of the user-defined class, the left operand would need to be an object of that class. However, cout and cin are stream objects, not objects of our class. Therefore, non-member functions are used.
यदि इन operators को user-defined class के सामान्य member functions के रूप में overload किया जाए, तो left operand उसी class का object होना चाहिए। लेकिन cout और cin stream objects हैं, हमारी class के objects नहीं। इसलिए इन्हें non-member functions के रूप में overload किया जाता है।
Friend functions are useful because they can access the private data members of the class while remaining non-member functions.
Friend functions उपयोगी होते हैं क्योंकि ये non-member functions होते हुए भी class के private data members को access कर सकते हैं।
Syntax of Stream Insertion Operator Overloading (<<)
The general syntax for overloading the stream insertion operator is:
Stream Insertion Operator को overload करने का सामान्य syntax निम्नलिखित है:
friend ostream& operator<<(ostream& out,const ClassName& obj);
The function is generally defined outside the class:
इस function को सामान्यतः class के बाहर define किया जाता है:
ostream& operator<<(ostream& out,const ClassName& obj) {out << obj.data;return out;}
Here:
ostream&is the return type.operator<<is the overloaded operator function.outrepresents the output stream.objrepresents the object whose data is displayed.const ClassName&avoids unnecessary copying and prevents modification of the object.return outreturns the output stream reference.
यहाँ:
ostream&return type है।operator<<overloaded operator function है।outoutput stream को दर्शाता है।objउस object को दर्शाता है जिसका data display किया जाता है।const ClassName&unnecessary copying से बचाता है और object में बदलाव को रोकता है।return outoutput stream का reference return करता है।
Syntax of Stream Extraction Operator Overloading (>>)
The general syntax for overloading the stream extraction operator is:
Stream Extraction Operator को overload करने का सामान्य syntax निम्नलिखित है:
friend istream& operator>>(istream& in,ClassName& obj);
The function is generally defined outside the class:
इस function को सामान्यतः class के बाहर define किया जाता है:
istream& operator>>(istream& in,ClassName& obj) {in >> obj.data;return in;}
Here:
istream&is the return type.operator>>is the overloaded operator function.inrepresents the input stream.objrepresents the object into which data is stored.ClassName&allows the function to modify the object's data.return inreturns the input stream reference.
यहाँ:
istream&return type है।operator>>overloaded operator function है।ininput stream को दर्शाता है।objउस object को दर्शाता है जिसमें data store किया जाता है।ClassName&function को object के data में बदलाव करने की अनुमति देता है।return ininput stream का reference return करता है।
C++ Program to Overload Stream Insertion and Extraction Operators
The following program demonstrates how to overload both << and >> operators using friend functions. A class named Student is created with two data members: rollNo and marks.
निम्नलिखित program में friend functions का उपयोग करके << और >> दोनों operators को overload करना दिखाया गया है। इसमें Student नाम की class बनाई गई है, जिसमें दो data members rollNo और marks हैं।
Program: Input and Display Student Details Using cin and cout
#include <iostream>using namespace std;class Student {private:int rollNo;float marks;public:friend istream& operator>>(istream& in, Student& s);friend ostream& operator<<(ostream& out,const Student& s);};istream& operator>>(istream& in, Student& s) {cout << "Enter Roll Number: ";in >> s.rollNo;cout << "Enter Marks: ";in >> s.marks;return in;}ostream& operator<<(ostream& out, const Student& s) {out << "Roll Number: " << s.rollNo << endl;out << "Marks: " << s.marks << endl;return out;}int main() {Student s1;cin >> s1;cout << "\nStudent Details:\n";cout << s1;return 0;}
Output
Enter Roll Number: 101Enter Marks: 85.5Student Details:Roll Number: 101Marks: 85.5
Explanation of the Program
The
Studentclass contains two private data members:rollNoandmarks.The
operator>>function is declared as a friend function to accept input into the object's private data members.The
operator<<function is declared as a friend function to display the object's private data members.Both functions are defined outside the class.
The extraction operator function receives an input stream and a reference to the object.
The insertion operator function receives an output stream and a constant reference to the object.
Both functions return their respective stream references.
Studentclass में दो private data membersrollNoऔरmarksहैं।operator>>function को object के private data members में input लेने के लिए friend function के रूप में declare किया गया है।operator<<function को object के private data members को display करने के लिए friend function के रूप में declare किया गया है।दोनों functions को class के बाहर define किया गया है।
Extraction operator function एक input stream और object का reference प्राप्त करता है।
Insertion operator function एक output stream और object का constant reference प्राप्त करता है।
दोनों functions अपनी-अपनी stream का reference return करते हैं।
The statement cin >> s1; calls the overloaded extraction operator and stores the entered values in the object.
Statement cin >> s1; overloaded extraction operator को call करता है और entered values को object में store करता है।
Similarly, cout << s1; calls the overloaded insertion operator and displays the object's details.
इसी प्रकार, cout << s1; overloaded insertion operator को call करता है और object की details display करता है।
Chaining of Stream Insertion and Extraction Operators
One important advantage of returning the stream reference is that it allows operator chaining. This means we can use multiple insertion or extraction operations in a single statement.
Stream reference return करने का एक महत्वपूर्ण लाभ operator chaining है। इसका अर्थ है कि हम एक ही statement में कई insertion या extraction operations का उपयोग कर सकते हैं।
For example, insertion operator chaining:
उदाहरण के लिए, Insertion Operator Chaining:
cout << "Roll No: " << s1;Extraction operator chaining:
Extraction Operator Chaining:
cin >> a >> b;The overloaded operators return the same stream reference, allowing the next operation to continue on that stream.
Overloaded operators उसी stream का reference return करते हैं, जिससे अगला operation भी उसी stream पर continue हो सकता है।
Difference Between Stream Insertion and Stream Extraction Operators
Both operators are used for input and output operations, but they perform different tasks.
दोनों operators का उपयोग input और output operations के लिए किया जाता है, लेकिन इनके कार्य अलग-अलग होते हैं।
Stream Insertion (<<) | Stream Extraction (>>) |
|---|---|
| Used to display output. | Used to accept input. |
Commonly used with cout. | Commonly used with cin. |
| Sends data to an output stream. | Receives data from an input stream. |
| Generally takes a constant object reference. | Takes a non-constant object reference. |
Returns ostream&. | Returns istream&. |
Stream Insertion (<<) | Stream Extraction (>>) |
|---|---|
| Output display करने के लिए उपयोग होता है। | Input प्राप्त करने के लिए उपयोग होता है। |
सामान्यतः cout के साथ उपयोग होता है। | सामान्यतः cin के साथ उपयोग होता है। |
| Output stream में data भेजता है। | Input stream से data प्राप्त करता है। |
| सामान्यतः constant object reference लेता है। | Non-constant object reference लेता है। |
ostream& return करता है। | istream& return करता है। |
Rules for Overloading Stream Operators
The following guidelines should be followed when overloading stream insertion and extraction operators in C++.
C++ में Stream Insertion और Extraction operators को overload करते समय निम्नलिखित guidelines का पालन करना चाहिए।
Stream insertion and extraction operators are generally overloaded as non-member functions.
Friend functions are commonly used when direct access to private data members is required.
The insertion operator should return
ostream&.The extraction operator should return
istream&.The insertion operator generally accepts the object as a
constreference.The extraction operator accepts the object as a non-constant reference because it modifies the object's data.
Returning the stream reference supports chaining.
Stream Insertion और Extraction operators को सामान्यतः non-member functions के रूप में overload किया जाता है।
जब private data members को सीधे access करने की आवश्यकता हो, तो सामान्यतः friend functions का उपयोग किया जाता है।
Insertion operator को
ostream&return करना चाहिए।Extraction operator को
istream&return करना चाहिए।Insertion operator सामान्यतः object को
constreference के रूप में प्राप्त करता है।Extraction operator object को non-constant reference के रूप में प्राप्त करता है, क्योंकि यह object के data में बदलाव करता है।
Stream reference return करने से chaining संभव होती है।
Applications of Stream Operator Overloading
Stream operator overloading makes input and output operations more convenient for user-defined objects.
Stream Operator Overloading user-defined objects के लिए input और output operations को अधिक सुविधाजनक बनाता है।
Some common applications include:
कुछ सामान्य applications निम्नलिखित हैं:
Displaying student and employee records.
Accepting data for objects of user-defined classes.
Displaying complex numbers, dates, and time values.
Reading and displaying measurements and other custom data types.
Making class objects easier to use with standard C++ streams.
Student और employee records को display करना।
User-defined classes के objects के लिए data input करना।
Complex numbers, dates और time values को display करना।
Measurements और अन्य custom data types को read और display करना।
Class objects को standard C++ streams के साथ उपयोग करना आसान बनाना।
Frequently Asked Questions (FAQs)
1. What is stream insertion operator overloading in C++?
Stream insertion operator overloading allows us to use the << operator with user-defined objects to display their data using cout.
Stream Insertion Operator Overloading हमें user-defined objects के साथ << operator का उपयोग करके cout की सहायता से उनका data display करने की सुविधा देता है।
2. What is stream extraction operator overloading in C++?
Stream extraction operator overloading allows us to use the >> operator with user-defined objects to accept input using cin.
Stream Extraction Operator Overloading हमें user-defined objects के साथ >> operator का उपयोग करके cin की सहायता से input लेने की सुविधा देता है।
3. Why are stream operators generally overloaded using friend functions?
Because the left operand is a stream object such as cout or cin, not an object of the user-defined class. Non-member functions allow the stream to remain the left operand.
क्योंकि इन operators का left operand cout या cin जैसा stream object होता है, न कि user-defined class का object। Non-member functions stream को left operand बनाए रखने की सुविधा देते हैं।
4. What is the return type of the overloaded insertion operator?
The overloaded insertion operator generally returns ostream&.
Overloaded Insertion Operator सामान्यतः ostream& return करता है।
5. What is the return type of the overloaded extraction operator?
The overloaded extraction operator generally returns istream&.
Overloaded Extraction Operator सामान्यतः istream& return करता है।
6. Why do we return a reference to the stream?
Returning a stream reference allows chaining, such as cout << a << b and cin >> a >> b.
Stream का reference return करने से chaining संभव होती है, जैसे cout << a << b और cin >> a >> b।
7. Can stream insertion and extraction operators be overloaded as member functions of our class?
For the usual cout << obj and cin >> obj syntax, they are generally implemented as non-member functions because the left operand is a stream object.
सामान्य cout << obj और cin >> obj syntax के लिए इन्हें सामान्यतः non-member functions के रूप में implement किया जाता है, क्योंकि left operand एक stream object होता है।
Stream insertion and extraction operator overloading is an important application of operator overloading in C++. It allows programmers to use cout and cin directly with user-defined objects. By overloading << and >>, we can make input and output operations simpler, more readable, and consistent with standard C++ programming practices.
Stream Insertion और Extraction Operator Overloading C++ में Operator Overloading का एक महत्वपूर्ण उपयोग है। इसकी सहायता से programmers cout और cin का उपयोग user-defined objects के साथ सीधे कर सकते हैं। << और >> operators को overload करके हम input और output operations को सरल, readable और standard C++ programming practices के अनुरूप बना सकते हैं।
Comments
Post a Comment