title: When to mark a function in C++ as a virtual?
body: <p>Because of C++ nature of static-binding for methods, this affects the polymorphic calls.</p>
  
  <p>From Wikipedia:</p>
  
  <blockquote>
    <p>Although the overhead involved in this dispatch mechanism is low, it
    may still be significant for some application areas that the language
    was designed to target. For this reason, Bjarne Stroustrup, the
    designer of C++, elected to make dynamic dispatch optional and
    non-default. Only functions declared with the virtual keyword will be
    dispatched based on the runtime type of the object; other functions
    will be dispatched based on the object's static type.</p>
  </blockquote>
  
  <p>So the code:</p>
  
  <pre><code>Polygon* p = new Triangle;
  p-&gt;area();
  </code></pre>
  
  <p>provided that <code>area()</code> is a <code>non-virtual</code> function in Parent class that is <code>overridden</code> in the Child class, the code above will call the <code>Parent's class method</code> which might not be expected by the developer. (thanks to the static-binding I've introduced)</p>
  
  <p><strong>So, If I want to write a class to be used by others (e.g library), should I make all my functions to be virtual for the such previous code to run as expected?</strong></p>