C++快速入门 第二十七讲:运算符左移<<重载
一般来说,在调用operator<<()重载函数时,传递给它的是哪一个流,它返回的就应该是那个流的一个引用。
实例:左移操作符重载
1 #include <iostream> 2 #include <string> 3 #include <math.h> 4 5 using namespace std; 6 class Rational//定义基类 7 ; 24 25 Rational::Rational(int num,int den)//构造函数实现 26 32 //normalize()对分数进行简化操作包括: 33 //1.只允许分子为负数,如果分母为负数则把负数挪到分子部分,如1/2==1/2 34 //2.利用欧几里德算法(辗转求余原理)将分数进行简化:2/10 => 1/5 35 36 void Rational::normalize() 37 44 //欧几里德算法 45 int a = abs(numerator); 46 int b = abs(deninator); 47 48 //求出最大公约数 49 while(b>0) 50 55 56 //分子、分母分别除以最大公约数得到最简化分数 57 numerator /= a; 58 deninator /= a; 59 } 60 //a c a*d c*b a*d + c*d 61 // + = + = 62 //b d b*d b*d b*d Rational Rational::operator+(Rational rhs)//分数的加运算 64 75 //a c a c 76 // = + 77 //b d b d 78 Rational Rational::operator(Rational rhs)//分数的减运算 79 83 //a c a*c 84 // * = 85 //b d b*d 86 Rational Rational::operator*(Rational rhs)//分数的乘运算 87 98 //a c a d 99 // / = * 100 //b d b c 101 Rational Rational::operator/(Rational rhs)//分数的除运算 102 110 111 ostream& operator<<(ostream& os,Rational f);//函数声明 112 113 int main() 114 132 ostream& operator<<(ostream& os,Rational f)//并不属于Rational类,是一个独立的函数 133C/C++
nsbtx文件怎么看,nsbtx文件用什么打开?
ess文件怎么看,ess文件用什么打开?
esproj文件怎么看,esproj文件用什么打开?
esps文件怎么看,esps文件用什么打开?
8st文件怎么看,8st文件用什么打开?
8med文件怎么看,8med文件用什么打开?
8li文件怎么看,8li文件用什么打开?
8cm文件怎么看,8cm文件用什么打开?
8bx文件怎么看,8bx文件用什么打开?
8by文件怎么看,8by文件用什么打开?