![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
Ah, I see. So a virtual class is like an abstract class in Java.
I'll post again when I have more specific questions. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
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.
__________________
Visit my website BinaryNotions. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| About pure virtual function calls | Laterix | C++ | 9 | May 26th, 2006 4:59 PM |
| What is the point of pure virtual functions? | aznluvsmc | C++ | 12 | Apr 14th, 2006 6:48 PM |
| Virtual Memory question | metsfan | C | 2 | Apr 11th, 2006 6:47 PM |
| Why can't we have Virtual Constructors??? | kaustubhkane | C++ | 4 | Sep 21st, 2005 9:21 AM |
| Virtual functions on casted objects | Rei | C++ | 1 | Apr 10th, 2005 7:44 AM |