Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2007, 12:18 AM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Feb 28th, 2007, 2:33 AM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Feb 28th, 2007, 2:57 AM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Ah, I see. So a virtual class is like an abstract class in Java.

I'll post again when I have more specific questions.
titaniumdecoy is offline   Reply With Quote
Old Feb 28th, 2007, 4:54 AM   #4
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
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.
Eoin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:05 PM.

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