2

I have visited quite a few links by now.
I seem to understand how to use the keyword virtual.
But I still haven't got an insight as what it does to a function. ( leave alone classes!)

I have a few guesses as what it might do..
Please allow me to explain myself through the following code fragments

Classes

  class stars 
{ 
    public :
        void describe { cout << "Stars are bright objects\n"}
};

  class sun : public stars
{
    public: 
        void describe { cout << "Sun is visibly the most brightest star\n";}
};

Main Function

  void main()
{
    sun *ptr = new sun;   
     stars *spt = ptr;
    ptr -> describe();        //Output line1: "Sun is visibly the most brightest star"  
    spt->describe();          //Output line2: "Stars are bright objects"
    ((sun*)stp) -> describe() //Output line3: "Sun is visibly the most brightest star"
}

Now, had I used virtual before void describe() in class stars. The second line would have been different. I believe it's because, the keyword virtual influences the way the function works.
(i.e) while executing stp -> describe(); the function stars::describe() finds the object type associated with the address stored in stp, and accordingly prioritizes the function to be called.

Please comment, if I didn't make myself clear (this is my first question)
Thank you for sparing your precious time for reading this, I really acknowledge it

  • 3
    Welcome to Stack Overflow! Stack Overflow isn't really a good place to understand the basics of a language. If you are new to C++, and curious on how it works, you would be well served by getting a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Dec 17 '18 at 20:17
  • https://en.wikipedia.org/wiki/Virtual_method_table – zzxyz Dec 17 '18 at 20:20
  • Do you want to know how virtual is implemented ? (with vtable most of the time). – Jarod42 Dec 17 '18 at 20:20
  • Probably I don't know how to phrase the question. Please go through my code before down voting me for my tittle – Adorable Cherry Dec 17 '18 at 20:23
  • Exactly! I wanna know how it works @Jarod42 – Adorable Cherry Dec 17 '18 at 20:25
  • @AdorableCherry I hope you can see - it's difficult to provide a definitive answer to your question, since you say you've read up on the topic, and you're specifically looking for an explanation that's somehow different from the explanations you've already read. Have you looked at the duplicate link? – Drew Dormann Dec 17 '18 at 20:29
  • I would haven't been upset if you had marked my question duplicate by suggesting this link https://stackoverflow.com/questions/99297/how-are-virtual-functions-and-vtable-implemented. – Adorable Cherry Dec 17 '18 at 20:38
  • @DrewDormann Sorry... but all I want is an intuitive explanation for how virtual functions are implemented. And I went through the original one before asking this question. I will keep my questions short from here on. – Adorable Cherry Dec 17 '18 at 20:47
  • @AdorableCherry no need to be sorry! The size of your question is not an issue, to be clear. – Drew Dormann Dec 17 '18 at 20:53

0 Answers0