title: virtual function and derived class
body: <p>Let me show you the simple C++ code example first.</p>
  
  <pre><code>#include &lt;iostream&gt; 
  using namespace std;
  
  class Person{
  
    string m_name;
  public:
    virtual void saySomething(){
      cout &lt;&lt; "I am a person.";
    }
  };
  
  class Student:public Person{
  public:
  
    void saySomething(){
      cout &lt;&lt; "I am a student." &lt;&lt; endl;
    }
  };
  
  class CSStudent:public Student{
  public:
  
    void saySomething(){
      cout &lt;&lt; "I am a CS Student." &lt;&lt; endl;
    }
  };
  
  int main(){
    Person* p = new Person();
    Student* s = new Student();
    CSStudent* c = new CSStudent();
  
    ((Student*)c)-&gt;saySomething();
  
    return 0;
  }
  </code></pre>
  
  <p>In my example, Student is a derived class of Person.
  Also, CSStudent is a derived class of Student.
  I know virtual keyword make it possible to determine if it is derived class.</p>
  
  <p>Question 1.</p>
  
  <p>((Student*)c)->saySomething();</p>
  
  <p>Why do I get "I am a CS Student."?
  I expected "I am a Student." since I specify virtual keyword only for the Person.</p>
  
  <p>Question 2.</p>
  
  <p>I saw an example that puts a virtual keyword to only base case.
  Also, I saw an example that puts virtual keywords to all base case and derived classes. What's the difference between these two?</p>
  
  <p>Question 3.</p>
  
  <p>virtual function is determined in run time.
  What about non-virtual function? Is it determined in compile time?</p>