title: Can functions accept abstract base classes as arguments?
body: <p>Having gotten comfortable with the idea of basic classes and encapsulation, I've launched myself towards understanding polymorphism, but I can't quite figure out how to make it work. Many of the examples I've searched through come across as really, <em>really</em> forced (classes Foo and Bar are just too abstract for me to see the utility), but here's how I understand the basic concept: you write a base class, derive a whole bunch of other things from it that change what the base methods <em>do</em> (but not what they "are"), then you can write general functions to accept and process any of the derived classes because you've somewhat standardized their appearance. With that premise, I've tried to implement the basic Animal->cat/dog hierarchy like so:</p>
  
  <pre><code>class Animal {
    public:
      virtual void speak() = 0;
  };
  
  class Dog : public Animal {
    public:
      void speak() {cout &lt;&lt; "Bark bark!" &lt;&lt; endl;}
  };
  
  class Cat : public Animal {
    public:
      void speak() {cout &lt;&lt; "Meow!" &lt;&lt; endl;}
  };
  
  void speakTo(Animal animal) {
      animal.speak();
  }
  </code></pre>
  
  <p>where speakTo can take can take a general kind of animal and make it, well, speak. But as I understand it, this doesn't work because I can't instantiate Animal (specifically in the function argument). I ask, then, do I understand the basic utility of polymorphism, and how can I <em>really</em> do what I've tried to do?</p>