Isn't this code have something special ?? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Isn't this code have something special ??

Refer code below and observe the output. It is base class which we get as output for both the pointers Now just uncomment getclassname method from both the class... No need to uncomment the code which calls this method. We are getting now different result. Why so ? https://code.sololearn.com/cHrjT5fPzu8D/?ref=app

14th May 2022, 3:44 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Réponses
+ 1
Hi Ketan! It happens because in the first case the base class is not polymorphic (class without any virtual member). In second case the class is polymorphic due to the method you just uncommented! To prove that you can just create a virtual method in base class to turn it polymorphic and run the code again
14th May 2022, 4:49 AM
Victor Cortes
Victor Cortes - avatar
+ 1
Thanks Victor Cortes ... I just tried with only having deafult virtual destructor in base class nd it worked... Thanks for your help
14th May 2022, 4:53 AM
Ketan Lalcheta
Ketan Lalcheta - avatar