Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

virtual functions

3 posters

Go down

virtual functions Empty virtual functions

Post by aarthi Thu Mar 04, 2010 11:49 am

Is virtual function same as the concept of overriding in java(we ll have a common method in the base class which will be overrided by the method in the derived clas )??what is the need for the keyword virtual???

aarthi

Posts : 12
Points : 22
Join date : 2010-02-26

Back to top Go down

virtual functions Empty Re: virtual functions

Post by Christopher Thu Mar 04, 2010 11:55 am

Virtual functions are required to achieve dynamic polymorphism. If derived class object is passed to a base class reference and overwritten function is called, base class function will be executed. So, to hide base class function, we go for vitual function.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

virtual functions Empty Re: virtual functions

Post by aarthi Thu Mar 04, 2010 12:40 pm

we can do overriding without using the keyword virtual itself....

aarthi

Posts : 12
Points : 22
Join date : 2010-02-26

Back to top Go down

virtual functions Empty Re: virtual functions

Post by Christopher Thu Mar 04, 2010 1:01 pm

Virtual functions are not just meant for overriding. It is used to invoke derived class member functions using base class reference. Overriding is little different, it is used to hide the implementation of base class function. But still overwritten function can be called only by derived class reference.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

virtual functions Empty Re: virtual functions

Post by crack2drop Thu Mar 04, 2010 1:17 pm

add up on his point........dynamically deciding upon which class to call -->>super class or sub class is the functionality of virtual functions.....for that u use reference pointers which refer to the instance of the class which contains the method

crack2drop

Posts : 4
Points : 4
Join date : 2010-03-03
Age : 36
Location : Chennai

Back to top Go down

virtual functions Empty Re: virtual functions

Post by aarthi Thu Mar 04, 2010 1:25 pm

Thank you!!!!

aarthi

Posts : 12
Points : 22
Join date : 2010-02-26

Back to top Go down

virtual functions Empty Re: virtual functions

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum