Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Virtual Confusion (http://www.programmingforums.org/showthread.php?t=12692)

titaniumdecoy Feb 28th, 2007 1:18 AM

Virtual Confusion
 
I have been experimenting with C++, and although I have a basic understanding of when to use the "virtual" keyword, it still confuses me. When do you make an entire class virtual rather than just its members? Also, where can I find programming challenges that test this understanding? Thanks.

bl00dninja Feb 28th, 2007 3:33 AM

you make an entire class "virtual" when you never expect it to be instantiated in an object form. like a class "human". all humans share traits, but there should probably be a child class (at least) for male and female.

class "shape" should be virtual. you never instantiate a "shape", but you might want to make a circle or a square, or other members that inherit from shape. area and volume might be private members of shape, but shape doesn't know how to deal with those.

crap like that.

titaniumdecoy Feb 28th, 2007 3:57 AM

Ah, I see. So a virtual class is like an abstract class in Java.

I'll post again when I have more specific questions.

Eoin Feb 28th, 2007 5:54 AM

Hi, this is just in case you are confusing terms, disregard otherwise.

To make an abstract class in C++ you give the class a pure virtual function. This link nicely explains the syntax for that- http://www.parashift.com/c++-faq-lit....html#faq-22.3.

Virtual inheritance is something different, it's used to avoid duplication when using multiple inheritance where there is a common base class. Again that C++ FAQ has a nice explanation- http://www.parashift.com/c++-faq-lit....html#faq-25.9.


All times are GMT -5. The time now is 1:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC