hello my dear friends


I have read a book that was simple to learn c++,recently

But in operator ovorloading threads ,I dont underestand many example for ovoreloading in another books & booklets

for example :
in the example below

1 // Fig. 11.3: PhoneNumber.h
2 // PhoneNumber class definition
3 #ifndef PHONENUMBER_H
4 #define PHONENUMBER_H
5
6 #include <iostream>
7 using std::ostream;
8 using std::istream;
9
10 #include <string>
11 using std::string;
12
13 class PhoneNumber
14 {
15 friend ostream &operator<<( ostream &, const PhoneNumber & );
16 friend istream &operator>>( istream &, PhoneNumber & );
17 private:
18 string areaCode; // 3-digit area code
19 string exchange; // 3-digit exchange
20 string line; // 4-digit line
21 }; // end class PhoneNumber
22
23 #endif


1 // Fig. 11.4: PhoneNumber.cpp
2 // Overloaded stream insertion and stream extraction operators
3 // for class PhoneNumber.
4 #include <iomanip>
5 using std::setw;
6
7 #include "PhoneNumber.h"
8
9 // overloaded stream insertion operator; cannot be
10 // a member function if we would like to invoke it with
11 // cout << somePhoneNumber;
12 ostream &operator<<( ostream &output, const PhoneNumber &number )
13 {
14 output << "(" << number.areaCode << ") "
15 << number.exchange << "-" << number.line;
16 return output; // enables cout << a << b << c;
17 } // end function operator<<
18
19 // overloaded stream extraction operator; cannot be
20 // a member function if we would like to invoke it with
21 // cin >> somePhoneNumber;
22 istream &operator>>( istream &input, PhoneNumber &number )
23 {
24 input.ignore(); // skip (
25 input >> setw( 3 ) >> number.areaCode; // input area code
26 input.ignore( 2 ); // skip ) and space
27 input >> setw( 3 ) >> number.exchange; // input exchange
28 input.ignore(); // skip dash (-)
29 input >> setw( 4 ) >> number.line; // input line
30 return input; // enables cin >> a >> b >> c;
31 } // end function operator>>


What are ostream & istream and what do they do?
Why output or input Been used. what do they do here? why not use cout??

and...

ostream & istream in line 15-16
output in line 14-16 section 2
input.ignore ?
what are these?


thanks a lot

>> What are ostream & istream and what do they do?

ostream stands for output stream, and istream stands for input stream. They are base classes that define the interface for any input/output stream. All the stream objects in the C++ standard library derive from one of these base classes or both. The reason they are used in this context (operator overloading) is because it allows the input/output operators (<< and >>) to be used for any actual type of stream object (this is called substitutability, an essential concept in polymorphism). This way, the same operator overload can be used for any type of streams (file streams, console streams (like cin / cout / cerr) and string streams). I suggest you read and explore this reference site, and this tutorial.

>> Why output or input Been used. what do they do here?

These are just the names that was chosen for the parameters to the operator overload.

>> why not use cout??

Because the output operator overload should work for any other output stream, not just cout.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.