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 423 — #461<br />

✐<br />

14.7. Her<strong>en</strong>cia y sobrecarga de operadores<br />

malm<strong>en</strong>te, heredará públicam<strong>en</strong>te para que el interfaz de la clase base sea también<br />

el interfaz de la clase derivada. Sin embargo, puede usar la palabra clave protected<br />

durante la her<strong>en</strong>cia.<br />

Derivar de forma protegida significa "implem<strong>en</strong>tado <strong>en</strong> términos de" para otras<br />

clases pero "es-una" para las clases derivadas y amigas. Es algo que no utilizará muy<br />

a m<strong>en</strong>udo, pero esta <strong>en</strong> el l<strong>en</strong>guaje para completarlo.<br />

14.7. Her<strong>en</strong>cia y sobrecarga de operadores<br />

Excepto el operador de asignación, el resto de operadores son heredados automáticam<strong>en</strong>te<br />

<strong>en</strong> la clase derivada. Esto se puede demostrar heredando de C12:Byte.h:<br />

//: C14:OperatorInheritance.cpp<br />

// Inheriting overloaded operators<br />

#include "../C12/Byte.h"<br />

#include <br />

using namespace std;<br />

ofstream out("ByteTest.out");<br />

class Byte2 : public Byte {<br />

public:<br />

// Constructors don’t inherit:<br />

Byte2(unsigned char bb = 0) : Byte(bb) {}<br />

// operator= does not inherit, but<br />

// is synthesized for memberwise assignm<strong>en</strong>t.<br />

// However, only the SameType = SameType<br />

// operator= is synthesized, so you have to<br />

// make the others explicitly:<br />

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

Byte::operator=(right);<br />

return *this;<br />

}<br />

Byte2& operator=(int i) {<br />

Byte::operator=(i);<br />

return *this;<br />

}<br />

};<br />

// Similar test function as in C12:ByteTest.cpp:<br />

void k(Byte2& b1, Byte2& b2) {<br />

b1 = b1 * b2 + b2 % b1;<br />

#define TRY2(OP) \<br />

out

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

Saved successfully!

Ooh no, something went wrong!