// Phones2.cpp - Example of simple inheritance *with* polymorphism.
// From an idea of Coplein's "Advanced C++ Styles and Idioms", 1992,
// page 93.  Note many details have been omitted for the sake of clarity.
//
// Written by Wayne Pollock, Tampa Florida USA, 2000.

class Telephone
{
public:
   virtual void ring ();
   virtual bool isOnHook ();
   virtual bool isTalking ();
   virtual bool isDialing ();
   LineNumber get_extension ();
   virtual ~Telephone ();

protected:
   Telephone ();  // Trick so only derived classes can invoke the constructor.
                  // (A poor-man's abstract class; See 3rd version for a
                  // better way!)
   LineNumber extension;
};

class POTSPhone : public Telephone  // POTS = Plain Old Telephone Service
{ ...  };

class ISDNPhone : public Telephone  // Integrated Services digital Network
{ ...  };

class OperatorPhone : public ISDNPhone   // Note: inherits from ISDNPhone.
{
   void ring () { ... }
   ...
};

class PrincessPhone : public POTSPhone  // A kind of POTSPhone.
{ ... };

void ring_phones ( Telephone* phone_list [] )
{
   for ( Telephone* p = phone_list; p; ++p )  // End of list marked with NULL
      p->ring();
}

int main ()
{
   Telephone* phone_array[10];
   int i = 0;
   phone_array[i++] = new POTSPhone( "5384" );
   phone_array[i++] = new ISDNPhone( "1050" );
   phone_array[i++] = new OperatorPhone( "0" );
   phone_array[i++] = new POTSPhone( "5385" );
   ...
   phone_array[i++] = NULL;
   ring_phones( phone_array );
   ...
   delete phone_array[0];  // Note correct destructor is called!
   ...
   return 0;
}
