944,156 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1106
  • C RSS
Mar 24th, 2006
0

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

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jack223 is offline Offline
27 posts
since Nov 2005
Mar 24th, 2006
0

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

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Opening Image file?
Next Thread in C Forum Timeline: Reducing if statements..again





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC