Vererbung in C++ [] (Vererbung in C++), Lektion, page 722682
https://www.purl.org/stefan_ram/pub/c++_vererbung (permalink) is the canonical URI of this page.
Stefan Ram
C++-Kurs

Vererbung in C++ 

Beispiel

Es wird zunächst offengelassen, wie der Ausgabestrom implementiert wird.

writeAlphaTo arbeitet auf einem Obertyp (einer Schnittstelle).

main.cpp
#include <ostream>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>

void writeAlphaTo( ::std::ostream & out ){ out << "alpha\n"; }

int main()
{ { ::std::ostringstream string_sink;
writeAlphaTo( string_sink );
::std::cout << "string_sink: " << string_sink.str(); }
{ ::std::ofstream file( "tmp.txt" );
writeAlphaTo( file ); }
{ ::std::cout << "file: " << ::std::ifstream( "tmp.txt" ).rdbuf(); }
{ writeAlphaTo( ::std::cout ); }}
::std::cout
string_sink: alpha
file: alpha
alpha

Werden die inneren geschweiften Klammer in »main« entfernt, so wird die Dateiausgabe nicht mehr unbedingt sichtbar, da die Datei dann noch nicht geschlossen wurde, wenn sie wieder ausgelesen werden soll. Dies ist mal ein Beispiel dafür, daß ein innerer Block in C++  eine Bedeutung hat.

Vererbung
Algorithmus auf Schnittstelle                          writeAlphaTo
|
c
Obertyp/Schnittstelle ostream
^
|
.-------------------------'-------------------------.
| | |
Untertypen/Implementation ostringstream | ofstream
|
Exemplare ::std::cout

Da ostringstream und ofstream beides Untertypen von ostream sind, dürfen ihre Ausdrücke verwendet werden, wo eine Ausdruck vom Typ ostream erlaubt ist. ::std::cout ist ein Exemplar eines Typs, der ostream oder eine Untertyp davon ist. Im Begriff „Algorithmus auf Schnittstelle“ ist das Wort „Algorithmus“ nicht im Sinne der C++ -Standardbibliothek gemeint.

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email-address of Stefan Ram.   |   A link to the start page of Stefan Ram appears at the top of this page behind the text "Stefan Ram".)  |   Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram722682 stefan_ram:722682 Vererbung in C++ Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd722682, slrprddef722682, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram.
https://www.purl.org/stefan_ram/pub/c++_vererbung