+ 1
What is the difference between an overloaded member function and a virtual function?
30 Respostas
+ 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.
+ 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.
+ 2
I hope I can help you. If you still don't understand, you can continue to ask me.
+ 1
oh so the virtual function ensures that the call is always to the child function☺ Thank you
+ 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
+ 1
that's 3 years, you'll prolly be better at my age
+ 1
Mpumalanga,
0
Thanks again, much appreciated... I'll try and go through this chapter and get back to you👏👏
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
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
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?
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
0
yeah, plus Python's Pygame you should try it too
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
0
Lol I'm not too bad, good luck to you
0
u amazing
0
😂😂😂✌✌✌
0
lol if u don't mind me asking how old r u
0
I'm 24😐
0
wow and u so far ahead and u still so young kinda lol I'm 21only