What is the difference between an overloaded member function and a virtual function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between an overloaded member function and a virtual function?

24th Apr 2018, 10:05 AM
Mashele Comfort
Mashele Comfort - avatar
30 Answers
+ 3
Not always... If we use the base class' object, it will still call the base class' function. Only when we use the derived class' object, it will call the derived class' object, no matter which type of pointer is pointing to it. We want to achieve polymorphism(let different object do the same thing in the different way), so we may use the base classes' pointer/reference to point/refer to the derived classes' object, such as #include<iostream> using namespace std; class Animal{ public: virtual void Say()const{ // Different animals have different sound. } }; class Cat:public Animal{ public: virtual void Say()const{ cout<<"Meow!"; } }cat1,cat2; class Dog:public Animal{ public: virtual void Say()const{ cout<<"Bark!"; } }dog1,dog2; int main() { Animal*my_pets[]{&cat1,&dog1,&cat2,&dog2}; for(auto animal:mt_pets) animal->Say(); return 0; } The output is "Meow!Bark!Meow!Bark!", but if we remove all the keyword "virtual", then each of my_pets, no matter what it is, will say nothing. Therefore, virtual function is useful when we want to achieve polymorphism. Furthermore, we'd better set class Animal as an abstract class, because we don't want to use "Animal" to describe the animal. I can say "I have a cat", but I don't say "I have an animal." --- This description is almost like nothing! Therefore, it's better to make it an abstract class. The way to make it is to make a function a pure virtual function. Like this: class Animal{ public: virtual void Say()const=0; /* the "=0" means that the function is a pure virtual function, which can make the class an abstract class. */ }; If we do so, we cannot create any object of "Animal", but we can create an object of "Cat", which is derived by "Animal". NOTE: If the derived class of an abstract class doesn't create ALL the definition of the "=0" functions, it will be an abstract class, too.
24th Apr 2018, 4:56 PM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
+ 2
// overloaded struct B1 { void F(){cout<<0;} }; struct D1:B1 { void F(){cout<<1;} }; B1*r1=new D1; r1->F(); //output: 0 // virtual struct B2 { virtual void F(){cout<<0;} }; struct D2:B2 { virtual void F(){cout<<1;} }; B2*r1=new D2; r2->F(); //output: 1 Overloading functions are compile-time polymorphism, however, virtual functions are runtime polymorphism.
24th Apr 2018, 11:02 AM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
+ 2
I hope I can help you. If you still don't understand, you can continue to ask me.
24th Apr 2018, 5:14 PM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
+ 1
oh so the virtual function ensures that the call is always to the child function☺ Thank you
24th Apr 2018, 4:23 PM
Mashele Comfort
Mashele Comfort - avatar
+ 1
hy I'm j new hear just started as u can c u really great at this thanks for following me what type of languages r u learning and for wat if I may ask coz I'm learning for animating/games and Web development thanks for listening I guess I'm Johnnie nice talking to u
24th Apr 2018, 7:44 PM
johnnie
johnnie - avatar
+ 1
that's 3 years, you'll prolly be better at my age
25th Apr 2018, 11:46 AM
Mashele Comfort
Mashele Comfort - avatar
+ 1
Mpumalanga,
27th Apr 2018, 10:54 AM
Mashele Comfort
Mashele Comfort - avatar
0
Thanks again, much appreciated... I'll try and go through this chapter and get back to you👏👏
24th Apr 2018, 5:32 PM
Mashele Comfort
Mashele Comfort - avatar
0
thanks for following me, anyway I'm in second year Computer Science, I'm not yet sure of which path to follow but I think Software Engineering or Data Science...thanks for following me again and welcome to SoloLearn
24th Apr 2018, 7:57 PM
Mashele Comfort
Mashele Comfort - avatar
0
wow that's amazing u really on another level compared to me this stuff is really hard and lots of stuff to remember I don't know anything really but I figure html is the first thing to learn but hopefully I get to were u r some day p's. johnnie
24th Apr 2018, 8:00 PM
johnnie
johnnie - avatar
0
I'm sure you will, plus I'm not that far ahead...which games are you learning for? the Sudoku level or the big ones?
24th Apr 2018, 8:05 PM
Mashele Comfort
Mashele Comfort - avatar
0
I would say the big ones but I must start small to get a idea of it or just to b able to do the basics I guess
24th Apr 2018, 8:10 PM
johnnie
johnnie - avatar
0
yeah, plus Python's Pygame you should try it too
24th Apr 2018, 8:12 PM
Mashele Comfort
Mashele Comfort - avatar
0
wow I'm really impressed of ur work u have learned so many languages that's very hard and impressive yoh I hope I succeed as well
24th Apr 2018, 8:12 PM
johnnie
johnnie - avatar
0
Lol I'm not too bad, good luck to you
24th Apr 2018, 8:14 PM
Mashele Comfort
Mashele Comfort - avatar
0
u amazing
24th Apr 2018, 8:16 PM
johnnie
johnnie - avatar
0
😂😂😂✌✌✌
24th Apr 2018, 8:17 PM
Mashele Comfort
Mashele Comfort - avatar
0
lol if u don't mind me asking how old r u
25th Apr 2018, 9:01 AM
johnnie
johnnie - avatar
0
I'm 24😐
25th Apr 2018, 9:15 AM
Mashele Comfort
Mashele Comfort - avatar
0
wow and u so far ahead and u still so young kinda lol I'm 21only
25th Apr 2018, 10:13 AM
johnnie
johnnie - avatar