I have another homework question that i'm not sure what the answer is.

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2005
Posts: 27
Reputation: jack223 is an unknown quantity at this point 
Solved Threads: 0
jack223 jack223 is offline Offline
Light Poster

I have another homework question that i'm not sure what the answer is.

 
0
  #1
Mar 24th, 2006
The class Chevrolet inherits from the class Automobile. Both have implemented a function called DownShift(). Suppose carPtr is declared as: Automobile *carPtr;
The carPtr object can point to both a Chevrolet or an Automobile. We want the call carPtr -> DownShift() to call the right version of DownShift based on what carPtr points to. We must:

a) declare DownShift virtual in class Chevrolet
b) declare DownShift virtual in class Automobile
c) make the inheritance public
d) make the inheritance private


I think the answers are "a & b".
If it's only one side virtual then the compiler chooses the wrong version, right?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,858
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: I have another homework question that i'm not sure what the answer is.

 
0
  #2
Mar 24th, 2006
You only need to declare a function as virtual at the highest level. Any child classes will implicitly declare the same function as virtual, so there's no need for an explicit declaration, but it never hurts to be explicit. So b is the required answer and a is optional.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 948 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC