1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| class Point { private : double X; double Y; public : Point( double dX=0.0, double dY=0.0 ) { X = dX; Y = dY; } double GetX() { return m_dX; } double GetY() { return m_dY; } }; |
1
2
3
4
| Point cPoint(5.0, 6.0); cout << "(" << cPoint.GetX() << ", " << cPoint.GetY() << ", " << cPoint.GetZ() << ")" ; |
1
2
| Point Point1(5.0, 6.0, 7.0); cout << Point1; |
cout << cPoint
. If the operator is <<, what are the operands? The left operand is the cout object, and the right operand is your Point class object. cout is actually an object of type ostream. Therefore, our overloaded function will look like this:
1
| friend ostream& operator<< (ostream &out, Point &Pointtemp); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| class Point { private : double X; double Y; public : Point( double dX=0.0, double dY=0.0 ) { X = dX; Y = dY; } friend ostream& operator<< (ostream &out, Point &Pointtemp); double GetX() { return X; } double GetY() { return Y; } }; ostream& operator<< (ostream &out, Point &Pointtemp) { // Since operator<< is a friend of the Point class, we can access // Point's members directly. out << "(" << Pointtemp.X << ", " << Pointtemp.Y << ")" ; return out; } |
cout << cPoint << endl;
cout << cPoint << endl;
, due to the precedence/associativity rules, it evaluates this expression as (cout << cPoint) << endl;
. cout << cPoint
calls our void-returning overloaded operator<< function, which returns void. Then the partially evaluated expression becomes: void << endl;
, which makes no sense!(cout << cPoint)
returns cout. Then our partially evaluated expression becomes: cout << endl;
, which then gets evaluated itself!
1
2
3
4
5
6
7
8
9
10
| int main() { Point cPoint1(2.0, 3.0); Point cPoint2(6.0, 7.0); using namespace std; cout << cPoint1 << " " << cPoint2 << endl; return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| class Point { private : double m_dX, m_dY, m_dZ; public : Point( double dX=0.0, double dY=0.0, double dZ=0.0) { m_dX = dX; m_dY = dY; m_dZ = dZ; } friend ostream& operator<< (ostream &out, Point &cPoint); friend istream& operator>> (istream &in, Point &cPoint); double GetX() { return m_dX; } double GetY() { return m_dY; } double GetZ() { return m_dZ; } }; ostream& operator<< (ostream &out, Point &cPoint) { // Since operator<< is a friend of the Point class, we can access // Point's members directly. out << "(" << cPoint.m_dX << ", " << cPoint.m_dY << ", " << cPoint.m_dZ << ")" ; return out; } istream& operator>> (istream &in, Point &cPoint) { in >> cPoint.m_dX; in >> cPoint.m_dY; in >> cPoint.m_dZ; return in; } |
1
2
3
4
5
6
7
8
9
10
11
12
| int main() { using namespace std; cout << "Enter a point: " << endl; Point cPoint; cin >> cPoint; cout << "You entered: " << cPoint << endl; return 0; } |
3.0 4.5 7.26
as input, the program produces the following result:
1
| friend ostream& operator<< (ostream &out, Point &Pointtemp); |
1
| friend ostream& operator<< (ostream &out, const Point &Pointtemp); |
0 comments: