Can someone explain me simply what is virtual fonction | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Can someone explain me simply what is virtual fonction

Im confused :(

5th Oct 2017, 1:59 AM
Galagann
11 Antworten
+ 1
Say you have two classes like Dog and Chiwawa and 'Chiwawa : public Dog' (aka inherits). Sure, both can bark so you would put a bark method in the Dog class. But saying that their bark is the same is just pure nonsense so you make the Bark() in the Dog to be virtual : thus overrideable. Now you need to define Bark() in both classes separetly. What this does, if you create a Chiwawa with a pointer to an ancestor (polymorphism) like Dog* chiwi = new Chiwawa(); And call chiwi->Bark(); It will do the Chiwawa classes Bark rather than the Dogs
5th Oct 2017, 5:50 AM
Norbivar
Norbivar - avatar
+ 3
@Norbivar, so the chiwi is an instance of Dog, or Chiwawa? I lost you there..help appreciated.
5th Oct 2017, 5:56 AM
Ipang
+ 3
@Norbivar, I had doubt because I see two classes in the line, Dog and Chiwawa, so the object willl always be created based from the class following the "new" is that correct? thanks for explaining mate :)
5th Oct 2017, 6:03 AM
Ipang
+ 3
@Norbivar, hope you don't mind me asking again, is it necessary for the Dog class to be virtual for such instantiation process? and when should we choose: Dog chiwi = new Chiwawa; over the regular way like: Chiwawa chiwi = new Chiwawa;
5th Oct 2017, 6:19 AM
Ipang
+ 3
@Norbivar, yeah I'm somewhat confused between abstract and virtual, and mixed up C++ and Java. I appreciate your time and understanding, and the answers, at least now I'm getting somewhere. Hey I guess your answer earned the best answer mark here, given the details of it :-) Big Thanks mate...
5th Oct 2017, 6:59 AM
Ipang
+ 3
@Galagann, the checkmark next to Norbivar answer would look lots better in green :-D
5th Oct 2017, 7:40 AM
Ipang
+ 2
chiwi is an instance (or object), chiwawa is the class :)
5th Oct 2017, 5:57 AM
Norbivar
Norbivar - avatar
+ 2
Exactly, but that is not a problem here since Dog is the parent from the Chiwawa class thus a pointer of the Dog class pointing to a Chiwawa is perfectly fine.
5th Oct 2017, 6:05 AM
Norbivar
Norbivar - avatar
+ 2
@Norbivar thank you ! :D
5th Oct 2017, 1:27 PM
Galagann
+ 1
Virtual functions come into play only when you are using a parent-type (e.g. Dog to Chiwawa object) pointer. To get terms correct : classes aren't virtual, only methods are (and inheritence but that doesn't matter now). Use this only if you want to store/handle overall Dogs together. You could have a lot more type of dogs all inheriting from Dog and you might want to store them in a single array/container. Since C++ is strongly typed, each container can contain ONE type of objects. Pointers to base class for example are one type of objects but allows you to (as in this case) behave differently if used with virtual functions depending on the actual object it is pointin to. (I understand that this is a bit confusing now, but it's not hard, just overwhelming :) )
5th Oct 2017, 6:35 AM
Norbivar
Norbivar - avatar
+ 1
Only classes are abstract and only functions can be virtual. :) In C++, abstract classes are which have at least one PURE virtual function, that is they do not have a definition in the class they are declared in. Now logically, you cannot instantiate from a class which has an undefined method as such would be like a null pointer, which is bad. So it can only be inherited from, but if you want to instantiate those, they must define (effectively override) that/those (pure) virtual functions. Cheers mate, glad to be of assistance, anytime :)
5th Oct 2017, 7:07 AM
Norbivar
Norbivar - avatar