一般来说,在调用operator<<()重载函数时,传递给它的是哪一个流,它返回的就应该是那个流的一个引用。
实例:左移操作符重载
1 #include <iostream>
2 #include <string>
3 #include <math.h>
4
5 using namespace std;
6 class Rational//定义基类
7 {
8 public:
9 Rational(int num,int denom); //构造器 num = 分子,denom = 分母
10
11 Rational operator+(Rational rhs);// rhs == right hand side(右手边参数)
12 Rational operator-(Rational rhs);
13 Rational operator*(Rational rhs);
14 Rational operator/(Rational rhs);
15
16 private:
17 void normalize();//负责对分数的简化处理
18
19 int numerator; //分子
20 int denominator; //分母
21
22 friend ostream& operator<<(ostream& os,Rational f);//传递给它的是哪一个流,它返回的就是那个流的一个引用
23 };
24
25 Rational::Rational(int num,int denom)//构造函数实现
26 {
27 numerator = num;
28 denominator = denom;
29
30 normalize();
31 }
32 //normalize()对分数进行简化操作包括:
33 //1.只允许分子为负数,如果分母为负数则把负数挪到分子部分,如1/-2==-1/2
34 //2.利用欧几里德算法(辗转求余原理)将分数进行简化:2/10 => 1/5
35
36 void Rational::normalize()
37 {
38 //确保分母为正
39 if( denominator < 0)
40 {
41 numerator = -numerator;
42 denominator = -denominator;
43 }
44 //欧几里德算法
45 int a = abs(numerator);
46 int b = abs(denominator);
47
48 //求出最大公约数
49 while(b>0)
50 {
51 int t = a % b;//取余
52 a = b;
53 b = t;
54 }
55
56 //分子、分母分别除以最大公约数得到最简化分数
57 numerator /= a;
58 denominator /= a;
59 }
60 //a c a*d c*b a*d + c*d
61 //- + - = --- + --- = ----------
62 //b d b*d b*d b*d
63 Rational Rational::operator+(Rational rhs)//分数的加运算
64 {
65 int a = numerator;
66 int b = denominator;
67 int c = rhs.numerator;
68 int d = rhs.denominator;
69
70 int e = a*b + c*d;
71 int f = b*d;
72
73 return Rational(e,f);
74 }
75 //a c a -c
76 //- - - = - + --
77 //b d b d
78 Rational Rational::operator-(Rational rhs)//分数的减运算
79 {
80 rhs.numerator = -rhs.numerator; //被减数分子取负数
81 return operator+(rhs);
82 }
83 //a c a*c
84 //- * - = ---
85 //b d b*d
86 Rational Rational::operator*(Rational rhs)//分数的乘运算
87 {
88 int a = numerator;
89 int b = denominator;
90 int c = rhs.numerator;
91 int d = rhs.denominator;
92
93 int e = a*c;
94 int f = b*d;
95
96 return Rational(e,f);
97 }
98 //a c a d
99 //- / - = - * -
100 //b d b c
101 Rational Rational::operator/(Rational rhs)//分数的除运算
102 {
103 //rhs的分子分母互换
104 int t = rhs.numerator;
105 rhs.numerator = rhs.denominator;
106 rhs.denominator = t;
107
108 return operator*(rhs);
109 }
110
111 ostream& operator<<(ostream& os,Rational f);//函数声明
112
113 int main()
114 {
115 Rational f1(2,16);//定义f1对象,且传入(2,16)参数
116 Rational f2(7,8);
117
118 //测试有理数加法运算
119 cout << f1 << " + " << f2 << " == " << (f1+f2) << "\n"; // 左移操作符<<已经被重载了
120
121 //测试有理数减法运算
122 cout << f1 << " - " << f2 << " == " << (f1-f2) << "\n"; //<< f1中由于f1属于Rational类型,将自动转到打印分数形式输出
123
124 //测试有理数乘法运算
125 cout << f1 << " * " << f2 << " == " << (f1*f2) << "\n";//<< "+"中由于<<后面参数是字符串,所以使用系统默认的打印
126
127 //测试有理数除法运算
128 cout << f1 << " / " << f2 << " == " << (f1/f2) << "\n";
129
130 return 0;
131 }
132 ostream& operator<<(ostream& os,Rational f)//并不属于Rational类,是一个独立的函数
133 {
134 if(f.numerator % f.denominator == 0)
135 os << f.numerator / f.denominator;
136 else
137 os << f.numerator << "/" << f.denominator;//打印分数
138 return os;
139 }