📄 chapter12.html
字号:
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> <font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font><<(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> <font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>>>(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#009900>// Assignments modify & return lvalue:</font>
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font>+=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font>-=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font>*=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font>/=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font>%=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font>^=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font>&=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font>|=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font>>>=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> Integer&
<font color=#0000ff>operator</font><<=(Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#009900>// Conditional operators return true/false:</font>
<font color=#0000ff>friend</font> <font color=#0000ff>int</font>
<font color=#0000ff>operator</font>==(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> <font color=#0000ff>int</font>
<font color=#0000ff>operator</font>!=(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> <font color=#0000ff>int</font>
<font color=#0000ff>operator</font><(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> <font color=#0000ff>int</font>
<font color=#0000ff>operator</font>>(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> <font color=#0000ff>int</font>
<font color=#0000ff>operator</font><=(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> <font color=#0000ff>int</font>
<font color=#0000ff>operator</font>>=(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> <font color=#0000ff>int</font>
<font color=#0000ff>operator</font>&&(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#0000ff>friend</font> <font color=#0000ff>int</font>
<font color=#0000ff>operator</font>||(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right);
<font color=#009900>// Write the contents to an ostream:</font>
<font color=#0000ff>void</font> print(std::ostream& os) <font color=#0000ff>const</font> { os << i; }
};
#endif <font color=#009900>// INTEGER_H ///:~</font></PRE></FONT></BLOCKQUOTE>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C12:Integer.cpp {O}</font>
<font color=#009900>// Implementation of overloaded operators</font>
#include <font color=#004488>"Integer.h"</font>
#include <font color=#004488>"../require.h"</font>
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>+(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> Integer(left.i + right.i);
}
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>-(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> Integer(left.i - right.i);
}
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>*(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> Integer(left.i * right.i);
}
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>/(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
require(right.i != 0, <font color=#004488>"divide by zero"</font>);
<font color=#0000ff>return</font> Integer(left.i / right.i);
}
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>%(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
require(right.i != 0, <font color=#004488>"modulo by zero"</font>);
<font color=#0000ff>return</font> Integer(left.i % right.i);
}
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>^(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> Integer(left.i ^ right.i);
}
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>&(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> Integer(left.i & right.i);
}
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>|(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> Integer(left.i | right.i);
}
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font><<(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> Integer(left.i << right.i);
}
<font color=#0000ff>const</font> Integer
<font color=#0000ff>operator</font>>>(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> Integer(left.i >> right.i);
}
<font color=#009900>// Assignments modify & return lvalue:</font>
Integer& <font color=#0000ff>operator</font>+=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i += right.i;
<font color=#0000ff>return</font> left;
}
Integer& <font color=#0000ff>operator</font>-=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i -= right.i;
<font color=#0000ff>return</font> left;
}
Integer& <font color=#0000ff>operator</font>*=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i *= right.i;
<font color=#0000ff>return</font> left;
}
Integer& <font color=#0000ff>operator</font>/=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
require(right.i != 0, <font color=#004488>"divide by zero"</font>);
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i /= right.i;
<font color=#0000ff>return</font> left;
}
Integer& <font color=#0000ff>operator</font>%=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
require(right.i != 0, <font color=#004488>"modulo by zero"</font>);
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i %= right.i;
<font color=#0000ff>return</font> left;
}
Integer& <font color=#0000ff>operator</font>^=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i ^= right.i;
<font color=#0000ff>return</font> left;
}
Integer& <font color=#0000ff>operator</font>&=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i &= right.i;
<font color=#0000ff>return</font> left;
}
Integer& <font color=#0000ff>operator</font>|=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i |= right.i;
<font color=#0000ff>return</font> left;
}
Integer& <font color=#0000ff>operator</font>>>=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i >>= right.i;
<font color=#0000ff>return</font> left;
}
Integer& <font color=#0000ff>operator</font><<=(Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>if</font>(&left == &right) {<font color=#009900>/* self-assignment */</font>}
left.i <<= right.i;
<font color=#0000ff>return</font> left;
}
<font color=#009900>// Conditional operators return true/false:</font>
<font color=#0000ff>int</font> <font color=#0000ff>operator</font>==(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> left.i == right.i;
}
<font color=#0000ff>int</font> <font color=#0000ff>operator</font>!=(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> left.i != right.i;
}
<font color=#0000ff>int</font> <font color=#0000ff>operator</font><(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> left.i < right.i;
}
<font color=#0000ff>int</font> <font color=#0000ff>operator</font>>(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> left.i > right.i;
}
<font color=#0000ff>int</font> <font color=#0000ff>operator</font><=(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> left.i <= right.i;
}
<font color=#0000ff>int</font> <font color=#0000ff>operator</font>>=(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> left.i >= right.i;
}
<font color=#0000ff>int</font> <font color=#0000ff>operator</font>&&(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> left.i && right.i;
}
<font color=#0000ff>int</font> <font color=#0000ff>operator</font>||(<font color=#0000ff>const</font> Integer& left,
<font color=#0000ff>const</font> Integer& right) {
<font color=#0000ff>return</font> left.i || right.i;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -