implicit vs explicit interfaces
What are the pros/cons of using implicit interfaces (Cases 2 and 3; templates) vs using explicit interfaces (Case 1; pointer to abstract class) in the following example?

Code that doesn't change:

    class CoolClass
    {
    public:
      virtual void doSomethingCool() = 0;
      virtual void worthless() = 0;
    };
    
    class CoolA : public CoolClass
    {
    public:
      virtual void doSomethingCool()
      { /* Do cool stuff that an A would do */ }
        
      virtual void worthless()
      { /* Worthless, but must be implemented */ }
    };
            
    class CoolB : public CoolClass
    {
    public:
      virtual void doSomethingCool()
      { /* Do cool stuff that a B would do */ }
        
      virtual void worthless()
      { /* Worthless, but must be implemented */ }
    };


Case 1: A non-templated class that takes a base-class pointer which provides an explicit interface:

    class CoolClassUser
    {
    public:  
      void useCoolClass(CoolClass * coolClass)
      { coolClass.doSomethingCool(); }
    };
    
    int main()
    {
      CoolA * c1 = new CoolClass;
      CoolB * c2 = new CoolClass;
      </br>
      CoolClassUser user;
      user.useCoolClass(c1);
      user.useCoolClass(c2);
      </br>
      return 0;
    }


Case 2: A templated class whose template type provides an implicit interface:

    template <typename T>
    class CoolClassUser
    {
    public:  
      void useCoolClass(T * coolClass)
      { coolClass->doSomethingCool(); }
    };
    
    int main()
    {
      CoolA * c1 = new CoolClass;
      CoolB * c2 = new CoolClass;
      </br>
      CoolClassUser<CoolClass> user;
      user.useCoolClass(c1);
      user.useCoolClass(c2);
      </br>
      return 0;
    }


Case 3: A templated class whose template type provides an implicit interface (this time, not deriving from <i>CoolClass</i>:  

    class RandomClass
    {
    public:
      void doSomethingCool()
      { /* Do cool stuff that a RandomClass would do */ }
    
      // I don't have to implement worthless()! Na na na na na!
    };
    
    template <typename T>
    class CoolClassUser
    {
    public:  
      void useCoolClass(T * coolClass)
      { coolClass->doSomethingCool(); }
    };
    
    int main()
    {
      RandomClass * c1 = new RandomClass;
      RandomClass * c2 = new RandomClass;
      </br>
      CoolClassUser<RandomClass> user;
      user.useCoolClass(c1);
      user.useCoolClass(c2);
      </br>
      return 0;
    }

Case 1 requires that the object being passed in to <i>useCoolClass()</i> be a child of CoolClass (and implement <i>worthless()</i>). Cases 2 and 3, on the other hand, will take <i>any</i> class that has a <i>doSomethingCool()</i> function.

If users of the code were always fine subclassing <i>CoolClass</i>, then Case 1 makes intuitive sense, since the <i>CoolClassUser</i> would always be expecting an implementation of a <i>CoolClass</i>. But assume this code will be part of an API framework, so I cannot predict if users will want to subclass <i>CoolClass</i> or roll their own class that has a <i>doSomethingCool()</i> function.

Some related posts:  

<http://stackoverflow.com/a/7264550/635125>  

implicit vs explicit interfaces
===

What are the pros/cons of using implicit interfaces (Cases 2 and 3; templates) vs using explicit interfaces (Case 1; pointer to abstract class) in the following example?

Code that doesn't change:

    class CoolClass
    {
    public:
      virtual void doSomethingCool() = 0;
      virtual void worthless() = 0;
    };
    
    class CoolA : public CoolClass
    {
    public:
      virtual void doSomethingCool()
      { /* Do cool stuff that an A would do */ }
        
      virtual void worthless()
      { /* Worthless, but must be implemented */ }
    };
            
    class CoolB : public CoolClass
    {
    public:
      virtual void doSomethingCool()
      { /* Do cool stuff that a B would do */ }
        
      virtual void worthless()
      { /* Worthless, but must be implemented */ }
    };


Case 1: A non-templated class that takes a base-class pointer which provides an explicit interface:

    class CoolClassUser
    {
    public:  
      void useCoolClass(CoolClass * coolClass)
      { coolClass.doSomethingCool(); }
    };
    
    int main()
    {
      CoolA * c1 = new CoolClass;
      CoolB * c2 = new CoolClass;
      </br>
      CoolClassUser user;
      user.useCoolClass(c1);
      user.useCoolClass(c2);
      </br>
      return 0;
    }


Case 2: A templated class whose template type provides an implicit interface:

    template <typename T>
    class CoolClassUser
    {
    public:  
      void useCoolClass(T * coolClass)
      { coolClass->doSomethingCool(); }
    };
    
    int main()
    {
      CoolA * c1 = new CoolClass;
      CoolB * c2 = new CoolClass;
      </br>
      CoolClassUser<CoolClass> user;
      user.useCoolClass(c1);
      user.useCoolClass(c2);
      </br>
      return 0;
    }


Case 3: A templated class whose template type provides an implicit interface (this time, not deriving from <i>CoolClass</i>:  

    class RandomClass
    {
    public:
      void doSomethingCool()
      { /* Do cool stuff that a RandomClass would do */ }
    
      // I don't have to implement worthless()! Na na na na na!
    };
    
    template <typename T>
    class CoolClassUser
    {
    public:  
      void useCoolClass(T * coolClass)
      { coolClass->doSomethingCool(); }
    };
    
    int main()
    {
      RandomClass * c1 = new RandomClass;
      RandomClass * c2 = new RandomClass;
      </br>
      CoolClassUser<RandomClass> user;
      user.useCoolClass(c1);
      user.useCoolClass(c2);
      </br>
      return 0;
    }

Case 1 requires that the object being passed in to <i>useCoolClass()</i> be a child of CoolClass (and implement <i>worthless()</i>). Cases 2 and 3, on the other hand, will take <i>any</i> class that has a <i>doSomethingCool()</i> function.

If users of the code were always fine subclassing <i>CoolClass</i>, then Case 1 makes intuitive sense, since the <i>CoolClassUser</i> would always be expecting an implementation of a <i>CoolClass</i>. But assume this code will be part of an API framework, so I cannot predict if users will want to subclass <i>CoolClass</i> or roll their own class that has a <i>doSomethingCool()</i> function.

Some related posts:  

<http://stackoverflow.com/a/7264550/635125>  
