| | |
I have another homework question that i'm not sure what the answer is.
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2005
Posts: 27
Reputation:
Solved Threads: 0
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?
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?
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
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Opening Image file?
- Next Thread: Reducing if statements..again
Views: 948 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






