Niranjan Parreira
About
-
Posted Questions
No Question(s) posted yet!
Posted Answers
Answer
Rules for Virtual Functions
Compile time (early binding) VS runtime (late binding) behavior of Virtual Functions
Consider the following simple program showing runtime behavior of virtual functions.
Output:
Explanation: Runtime polymorphism is achieved only through a pointer (or reference) of base class type. Also, a base class pointer can point to the objects of base class as well as to the objects of derived class. In above code, base class pointer ‘bptr’ contains the address of object ‘d’ of derived class.Late binding (Runtime) is done in accordance with the content of pointer (i.e. location pointed to by pointer) and Early binding (Compile time) is done according to the type of pointer, since print() function is declared with virtual keyword so it will be bound at runtime (output is print derived class as pointer is pointing to object of derived class) and show() is non-virtual so it will be bound during compile time (output is show base class as pointer is of base type).NOTE: If we have created a virtual function in the base class and it is being overridden in the derived class then we don’t need virtual keyword in the derived class, functions are automatically considered as virtual functions in the derived class.
Working of virtual functions (concept of VTABLE and VPTR)As discussed here, if a class contains a virtual function then compiler itself does two things.
Consider the example below:
Output:
Explanation: Initially, we create a pointer of type base class and initialize it with the address of the derived class object. When we create an object of the derived class, the compiler creates a pointer as a data member of the class containing the address of VTABLE of the derived class.
Similar concept of Late and Early Binding is used as in above example. For fun_1() function call, base class version of function is called, fun_2() is overridden in derived class so derived class version is called, fun_3() is not overridden in derived class and is virtual function so base class version is called, similarly fun_4() is not overridden so base class version is called.
NOTE: fun_4(int) in derived class is different from virtual function fun_4() in base class as prototypes of both the functions are different.
This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Answer is posted for the following question.
Answer
Frequency relationship (x-axis scaling) Up to the Nyquist frequency (half the sampling rate), the frequency of each values produced by the
Answer is posted for the following question.
How to plot fft octave?
Answer
Pro tip: Enter your dates to see qualifying Tripadvisor Plus savings and perks 1,733 properties in South Carolina Sort by: Recommended Lazy River
Answer is posted for the following question.
Coud you guide best lazy river in South Carolina?
Answer
Does your cat have a tendency to run away ? Some precautions to prevent your cat from running way; What to do if your cat runs away ?
Answer is posted for the following question.
What should i do if my cat runs away?
Answer
Best overall cleanser : Cetaphil Gentle Skin Cleanser · Best oil cleanser : Hada Labo Gokujyun Cleansing Oil · Best cleanser for acne prone skin :
Answer is posted for the following question.
Hmmm. Need help. Oh hi Best cleanser for combination skin australia?
Answer
How to Become a Chef ; Step 1 Find a job working in a restaurant kitchen ; Step 2 Get a high school diploma or GED ; Step 3 Go to culinary
Answer is posted for the following question.
How to become a chef?
Answer
Horimiya has been one of the only RA's that hasn't been painful to watch weekly due to how fast paced it is Also, the amount of animes made
Answer is posted for the following question.
Why did horimiya skip so much?
Answer
The cities of Evansville, Vincennes, Terre Haute, Bloomington, Columbus, Madison and Jeffersonville are all included in the 812 area code.
Answer is posted for the following question.
What is jeffersonville area code?