0

I don't understand a thingšŸ’€

I can't understand the nature of inheritance Code: https://sololearn.com/compiler-playground/cpsfaJE7ZAGo/?ref=app

10th Dec 2024, 7:03 PM
Intermediate Depression
Intermediate Depression - avatar
7 Answers
+ 5
Intermediate Depression you had given two times base* so it gave the same result. Instead you can write one base and another derived and you can understand the difference from the base class and derived class. The concept used here is pointers by pointing towards the object's address so to get the output of the function here it is pointed to the object and its function quack to print inside the quack function.
10th Dec 2024, 7:46 PM
Aysha
+ 2
Intermediate Depression yeah you told it correctly but the thing is I was trying to make you understand in terms of pointer in simple manner without the use of virtual function. Basically the virtual functions are used where the base class and derived class both have the same function name which confuses the compiler which one to call so to determine exactly which one to call.
13th Dec 2024, 7:32 AM
Aysha
+ 1
Try to debug and understand or break your code into small small parts then understood Never give up šŸ‘ Intermediate Depression
12th Dec 2024, 5:06 PM
Mohammed Qadir Khan
Mohammed Qadir Khan - avatar
+ 1
Mohammed Qadir Khan Already done šŸ˜Ž
13th Dec 2024, 5:05 AM
Intermediate Depression
Intermediate Depression - avatar
+ 1
Aysha Yea exctly! Here is a piece of info that took a lot of time and research to get! The reason virtual functions work is because they create what is called a V-table. A V-table is created for each class that has a virtual function or is derived and overrides the virtual function. A V-table is basically a bunch of pointers that points to the correct functions of each class (if the class overrides the func then it points to the overridded version, else it points to the one in the base) (I hope someone reads this and it saves them a lot of time, effort and research lol) function
13th Dec 2024, 8:50 AM
Intermediate Depression
Intermediate Depression - avatar
0
Aysha Thank you so much for you reply. Your explanation was very vague tbh, but now that I understand the code I can tell what you were trying to imply here. My question was about the behaviour itself! Why would a pointer pointing to derived access base instead? The answer to that is static binding! and to fix this we should simply use virtual functions which will force a dynamic binding, resulting in the desired output.
13th Dec 2024, 5:04 AM
Intermediate Depression
Intermediate Depression - avatar
0
Welcome and Good šŸ‘Intermediate Depression.
13th Dec 2024, 4:48 PM
Mohammed Qadir Khan
Mohammed Qadir Khan - avatar