Hi, i have create a base and derived class. when the reach method is call, will it call the base menthod or derived? i am confuse over it. thanks in advance.
You have to be more careful when posting code. The above code isn't even close to compiling, so I cannot be sure what you really intended. But if I fix all the "obvious" compile errors with what I believe you intended, the answer is the derived version.
I do not know if you intended reach to be non-virtual. I also do not know if you intended derived to inherit base privately. Regardless of what you intended, the answer is the same.
That is anti-OO. You should in that case name the derived class method differently if you want to be able to call the base class method from outside the object. You'll also need to inherit the base class publicly so the method is visible.
Hi jsmith. As well as specifying the common attributes of its derivatives, a class may specify the common behaviours of its derivatives. There may be an 'intersection' between behaviours of two classes. This intersecting behaviour should be factored out to a superclass.
class Vehicle
instance variables
X : Int
Y : Int
instance methods
procedure Travel( inX : Int, inY : Int )
X <- inX
Y <- inY
-----------------------------------------------
class Car extends Vehicle
instance variables
FuelUsed : Float
instance methods
procedure Travel( inX : Int, inY : Int, inFuelUsed : Float )
Vehicle::Travel( inX, inY )
FuelUsed <- FuelUsed + inFuelUsed
-----------------------------------------------
class Bicycle extends Vehicle
instance variables
Height : Int
HeightWeightRatio : Float
instance methods
procedure Travel( inX : Int, inY : Int, inWeight : Float )
Vehicle::Travel( inX, inY )
HeightWeightRatio <- Height / inWeight
-----------------------------------------------
Of course, the monitoring of fuel by the Car class and the monitoring of health by the Bicyle class could be treated as separate behaviours. But then the caller would have to remember to call the relevant methods each time Travel is called and it starts getting a bit scruffy. If fuel is used every time a car travels, then it is reasonable to include this in the actual behaviour of travelling.
By the way littleimps, that double colon operator '::' is used in C++.
I've just read my message back, and I'm sure you will agree that the attributes Bicycle.Height and Bicycle.HeightWeightRatio sound quite ridiculous! =o}
But I'm sure that you know what I mean when I make the point about "intersecting behaviour".
To improve this example, create a new class Rider, then make Height and HeightWeightRatio attributes of Rider, and Rider a property of Bicycle.
Yes, but did you read what OP is really trying to do? In your example, he wants to have a Car instance but call Vehicle's Travel method instead of Car's.
Well, good point. I don't know. The code OP gave originally was calling the method from main(), but the code was so far from compiling to begin with that I can't really say what the intention was for sure.