13.01.2015 Views

Pensar en C++ (Volumen 1) - Grupo ARCO

Pensar en C++ (Volumen 1) - Grupo ARCO

Pensar en C++ (Volumen 1) - Grupo ARCO

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

✐<br />

✐<br />

✐<br />

“Volum<strong>en</strong>1” — 2012/1/12 — 13:52 — page 343 — #381<br />

✐<br />

12.3. Operadores sobrecargables<br />

const Byte<br />

operator%(const Byte& right) const {<br />

require(right.b != 0, "modulo by zero");<br />

return Byte(b % right.b);<br />

}<br />

const Byte<br />

operator^(const Byte& right) const {<br />

return Byte(b ^ right.b);<br />

}<br />

const Byte<br />

operator&(const Byte& right) const {<br />

return Byte(b & right.b);<br />

}<br />

const Byte<br />

operator|(const Byte& right) const {<br />

return Byte(b | right.b);<br />

}<br />

const Byte<br />

operator> right.b);<br />

}<br />

// Assignm<strong>en</strong>ts modify & return lvalue.<br />

// operator= can only be a member function:<br />

Byte& operator=(const Byte& right) {<br />

// Handle self-assignm<strong>en</strong>t:<br />

if(this == &right) return *this;<br />

b = right.b;<br />

return *this;<br />

}<br />

Byte& operator+=(const Byte& right) {<br />

if(this == &right) {/* self-assignm<strong>en</strong>t */}<br />

b += right.b;<br />

return *this;<br />

}<br />

Byte& operator-=(const Byte& right) {<br />

if(this == &right) {/* self-assignm<strong>en</strong>t */}<br />

b -= right.b;<br />

return *this;<br />

}<br />

Byte& operator*=(const Byte& right) {<br />

if(this == &right) {/* self-assignm<strong>en</strong>t */}<br />

b *= right.b;<br />

return *this;<br />

}<br />

Byte& operator/=(const Byte& right) {<br />

require(right.b != 0, "divide by zero");<br />

if(this == &right) {/* self-assignm<strong>en</strong>t */}<br />

b /= right.b;<br />

return *this;<br />

}<br />

Byte& operator%=(const Byte& right) {<br />

require(right.b != 0, "modulo by zero");<br />

if(this == &right) {/* self-assignm<strong>en</strong>t */}<br />

b %= right.b;<br />

343<br />

✐<br />

✐<br />

✐<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!