#include <iostream>
struct Manoscritto
{
virtual ~Manoscritto( ) { }
// ...
virtual std:: ostream & memorizza_dati( std:: ostream & stm ) const // 1
{
std:: cout << "memorizza_dati for Manoscritto" ;
// TODO: replace the above with writing out the actual stuff
return stm ;
}
// ...
} ;
std:: ostream & operator << ( std:: ostream & stm, const Manoscritto& m ) // 2
{ return m.memorizza_dati ( stm) ; }
struct Lettera : public Manoscritto
{
// ...
virtual std:: ostream & memorizza_dati( std:: ostream & stm ) const override // 3
{
// call base class function if required
// Manoscritto::print(stm) ;
std:: cout << "memorizza_dati for Lettera" ;
// TODO: replace the above with writing out added stuff
return stm ;
}
// ...
} ;
int main( )
{
Manoscritto m1, m2 ;
Lettera l1, l2, l3 ;
Manoscritto* Elenco[ ] = { & m1, & l1, & l2, & m2, & l3 } ;
for ( Manoscritto* p : Elenco ) std:: cout << * p << '\n ' ;
std:: cout << '\n ' ;
// or:
for ( Manoscritto* p : Elenco ) p- > memorizza_dati( std:: cout ) << '\n ' ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKc3RydWN0IE1hbm9zY3JpdHRvCnsKICAgIHZpcnR1YWwgfk1hbm9zY3JpdHRvKCkge30KCiAgICAvLyAuLi4KCiAgICB2aXJ0dWFsIHN0ZDo6b3N0cmVhbSYgbWVtb3JpenphX2RhdGkoIHN0ZDo6b3N0cmVhbSYgc3RtICkgY29uc3QgLy8gMQogICAgewogICAgICAgIHN0ZDo6Y291dCA8PCAibWVtb3JpenphX2RhdGkgZm9yIE1hbm9zY3JpdHRvIiA7CiAgICAgICAgLy8gVE9ETzogcmVwbGFjZSB0aGUgYWJvdmUgd2l0aCB3cml0aW5nIG91dCB0aGUgYWN0dWFsIHN0dWZmCgogICAgICAgIHJldHVybiBzdG0gOwogICAgfQoKICAgIC8vIC4uLgp9OwoKc3RkOjpvc3RyZWFtJiBvcGVyYXRvciA8PCAoIHN0ZDo6b3N0cmVhbSYgc3RtLCBjb25zdCBNYW5vc2NyaXR0byYgbSApIC8vIDI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