virtual functions

Go down

virtual functions

Post by aarthi on 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

View user profile

Back to top Go down

Re: virtual functions

Post by Christopher on 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.
avatar
Christopher
Admin

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

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: virtual functions

Post by aarthi on 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

View user profile

Back to top Go down

Re: virtual functions

Post by Christopher on 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.
avatar
Christopher
Admin

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

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: virtual functions

Post by crack2drop on 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 : 30
Location : Chennai

View user profile

Back to top Go down

Re: virtual functions

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

Thank you!!!!

aarthi

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

View user profile

Back to top Go down

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