LISTING 1. A Good Use of Multiple Inheritance class list_node // an element of a list { list_node *next, *previous; //... }; class list { public: insert( list_node *p ); // insert *p into list //... }; class employee : public list_node // all employees can { // be list elements char *name; // other employee info goes here. }; class manager : public employee // a manager is an employee { list peons; // managed people }; class peon : public employee // a peon is an employee { // peon information goes here }; list the_list; list_node *p; p = new peon; // A peon IS-A employee, which IS-A // list_node. There's no type mismatch. this_list.insert( p ); // Put the peon in the list. p = new manager; // Managers work the same way. this_list.insert( p ); ---------------------------------------------------------------------- LISTING 2. Using A Mix-in Class class employee { string name; // other employee info goes here. }; class manager : public employee // a manager is an employee { list peons; // managed people }; class peon : public employee // a peon is an employee , public list_node // and is "listable" { // peon information goes here }; list the_list; list_node *p; p = new peon; // A peon IS-A list_node. // There's no type mismatch. this_list.insert( p ); // Put the peon in the list. p = new manager; // ILLEGAL, managers are not this_list.insert( p ); // list_nodes ---------------------------------------------------------------------- LISTING 3. Adding Elements to a set or Tree class ObjClassA; class set { public: void add( ObjClassA *node ); //... }; class ObjClassB; class tree { public: void add( ObjClassB *node ); //... }; class myAclass: public ObjClassA {...} class myBclass: public ObjClassB {...} set s; tree t; myAclass *ap = new myAclass; s.add(ap); myBclass *bp = new myBclass; t.add(bp); ---------------------------------------------------------------------- LISTING 4. Problematic-- A class node; class data_structure { public: // add a node of some sort to a data structure virtual void add_item( node *item ) = 0; //... }; //------------------------------------------------------------ class node { public: virtual void add_self_to_data_structure( data_structure *p ) = 0; virtual int compare ( node *item ) = 0; //... }; //------------------------------------------------------------ class tree : public data_structure { friend class tree_node; tree_node *root; public: virtual void add_item( node *item ); //... }; //------------------------------------------------------------ class tree_node : public node { friend class tree; tree_node *left_child, *right_child; protected: virtual int compare( node *item ) = 0; public: virtual void add_self_to_data_structure( data_structure *p ) { tree_node *root = ((tree *)data_structure)->root; // Code to actually add the new node (pointed to // by this) to the tree rooted at "root" goes here. // Call the virtual compare() function to compare // the key fields of two nodes: //... if( compare(root) < 0 ) // compare current node to root root = root->left_child; } }; //------------------------------------------------------------ void tree::add_item( node *item ) { // Must be declared beneath class tree_node definition // to avoid forward reference to add_self...(); ((tree_node *)item) ->add_self_to_data_structure( root ); } //------------------------------------------------------------ class string_tree_node : public tree_node { char *key; //... public: string_tree_node( char *s ){ key = strdup(s); } virtual int compare( node *item ) { return strcmp( key, ((tree_node *)item)->key ); } }