title: Are virtual functions the only way to achieve Runtime Polymorphism in C++?
body: <p>One of my friends asked me "How Runtime Polymorphism is achieved in C++?" I answered "By Inheritance"</p>
  
  <p>He said "No, it can be achieved only using virtual functions". </p>
  
  <p>So I gave him an example of the following code :-</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class A
  {
  public:
      int i;
      A(){i=100;}
  };
  
  class B : public A
  {
  public:
      int j;
      B(){i = -1; j = 99;}
  };
  
  void func(A&amp; myA)
  {
      cout&lt;&lt;myA.i &lt;&lt; endl;
  }
  
  int main()
  {
      B b;
      A* a = new B();
      func(*a);
      func(b);
      delete a;
      return 0;
  }
  </code></pre>
  
  <p>Here, function func() takes reference of A but we pass object of B and we can print the value of public member "i". He said it is compile time polymorphism.</p>
  
  <p>My questions are :-</p>
  
  <p>1) Is Runtime polymorphism  achieved only with virtual functions?</p>
  
  <p>2) Is the example above has runtime polymorphism or compile time?</p>
  
  <p>3) If I have the following code :-</p>
  
  <pre><code>void func2(A&amp; myA)
  {
      cout &lt;&lt; myA.i &lt;&lt; endl;
      // dynamic/static cast myA to myB
      cout&lt;&lt;myB.j &lt;&lt; endl;
  }
  </code></pre>
  
  <p>what kind of polymorphism is it? Or is it even polymorphism?</p>