Skip to main content
gamedev.net
🔒 Locked

inheritance, polymorphism...

Started by Rasmadrak 1 reply 300+ views
Original Post
Rasmadrak
Rasmadrak
Hi there! I'm curious if there is a way to access a parents "properties" from a child...? if I have a "parent class" and a "child class : public parent" , I can not access the functions from the parent... Real example: I'm creating a Vehicle class that has several instances of a Wheel class... but I wish to have the wheels read the position of its parent, the vehicle. Only way I can think of is by providing a pointer to the parent, say Update(vehicle &Car); OR am I simply stupid and should let the car do the updating? :) Car->UpdateWheels()... What do you think?
"Game Maker For Life, probably never professional thou." =)
SiCrane
SiCrane
You should be able to access the parent class member variables and functions just by using the name without needing a pointer. Ex:
class Parent {  public:    int i;};class Child : public Parent {  public:    void a_function(void) {      i = 0;    }};
TDragon
TDragon
Your question shouldn't really deal with inheritance or polymorphism the way you stated the example. Specifically, inheritance deals with "is a" or "is a type of" relationships, and in this case you can't say "a wheel is a type of vehicle". Making your class Wheel inherit from Vehicle does not follow good OOP practice. Rather, you can simply let your Vehicle class contain 4 (or as many as you prefer) Wheel member variables.

With respect to the second part of your post: letting the vehicle update the wheels is probably the thing to do in this case, through Wheel member functions such as Rotate() and such. The wheel shouldn't need to know its position; if it does, let the vehicle tell it.

Cheers,
Twilight Dragon

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.