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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

✐<br />

✐<br />

✐<br />

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

✐<br />

14.4. Ocultación de nombres<br />

convertir a cualquier cosa que desee.<br />

Un procedimi<strong>en</strong>to que a primera vista parece mejor es especializar la clase g<strong>en</strong>eral<br />

Stack utilizando la her<strong>en</strong>cia. Aquí esta un ejemplo que utiliza la clase del capítulo<br />

9.<br />

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

// Specializing the Stack class<br />

#include "../C09/Stack4.h"<br />

#include "../require.h"<br />

#include <br />

#include <br />

#include <br />

using namespace std;<br />

class StringStack : public Stack {<br />

public:<br />

void push(string* str) {<br />

Stack::push(str);<br />

}<br />

string* peek() const {<br />

return (string*)Stack::peek();<br />

}<br />

string* pop() {<br />

return (string*)Stack::pop();<br />

}<br />

~StringStack() {<br />

string* top = pop();<br />

while(top) {<br />

delete top;<br />

top = pop();<br />

}<br />

}<br />

};<br />

int main() {<br />

ifstream in("InheritStack.cpp");<br />

assure(in, "InheritStack.cpp");<br />

string line;<br />

StringStack textlines;<br />

while(getline(in, line))<br />

textlines.push(new string(line));<br />

string* s;<br />

while((s = textlines.pop()) != 0) { // No cast!<br />

cout

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

Saved successfully!

Ooh no, something went wrong!