![]() |
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.
|
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. |
Ah, I see. So a virtual class is like an abstract class in Java.
I'll post again when I have more specific questions. |
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