#include #include using namespace std; class base { int i; protected: virtual void print(ostream& ost) const {ost << i;} public: base(int initial_i): i(initial_i) {} virtual ~base() {} friend ostream& operator<<(ostream& ost, const base& b) { b.print(ost); return ost; } }; class derived: public base { int j; void print(ostream& ost) const {base::print(ost); ost << ", " << j;} public: derived(int initial_i, int initial_j): base(initial_i), j(initial_j) {} }; int main() { base b(10); const base *p = &b; cout << *p << "\n"; //operator<<(cout, *p) << "\n"; derived d(20, 30); p = &d; cout << *p << "\n"; //operator<<(cout, *p) << "\n"; return EXIT_SUCCESS; }