#include #include using namespace std; class node { virtual void print(ostream& ost) const = 0; public: node *next; node(node *initial_next): next(initial_next) {} virtual ~node() {} friend ostream& operator<<(ostream& ost, const node& n) { n.print(ost); return ost; } }; class cowboy: public node { int i; public: static cowboy *begin; cowboy(int initial_i): node(begin), i(initial_i) {begin = this;} protected: void print(ostream& ost) const {ost << "cowboy " << i;} }; cowboy *cowboy::begin = 0; class bank: public node { int j; public: static bank *begin; bank(int initial_j): node(begin), j(initial_j) {begin = this;} protected: void print(ostream& ost) const {ost << "bank " << j;} }; bank *bank::begin = 0; class cowboybank: public cowboy, public bank { int k; void print(ostream& ost) const { ost << "cowboybank "; cowboy::print(ost); ost << ", "; bank::print(ost); ost << ", " << k; } public: cowboybank(int initial_i, int initial_j, int initial_k) : cowboy(initial_i), bank(initial_j), k(initial_k) {} }; int main() { cowboy c1 = 40; bank b1 = 80; cowboybank cb1(30, 70, 100); cowboybank cb2(20, 60, 90); cowboy c2 = 10; bank b2 = 50; cout << "Here are the cowboys:\n"; for (const node *p = cowboy::begin; p != 0; p = p->next) { cout << *p << "\n"; } cout << "\n"; cout << "Here are the banks:\n"; for (const node *p = bank::begin; p != 0; p = p->next) { cout << *p << "\n"; } return EXIT_SUCCESS; }