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...