Hi guys, wondering if you could give me some advice concerning the way to use OOP.
For the purpose of an example, imagine that I am writing some code the that is concerned with moving and manipulating trains. Each train can hold a different number of carriages. I want the code I write to control several trains each with a different number of carriages.
Now from what I have read I would declare a carriage type and store the relevent data for that carriage within that type. That type would also have methods and functions to create carriages, move carriages, draw carriages etc.
Now here is the point I want clarification on. From what I have read I would then create a type train which would store a list of carriages that belong to that train. So every time I create a train it automatically creates carriages.
Am I right so far?
The problem I am having is that when it comes to dealing with the list of carraiges within the type Train, I always end up moving the methods that were originally in the carriage type (move, create, draw etc) into the train type as it seems difficult to access them otherwise.
So my question is this: Am I overcomplicating things and using OOP incorrectly or is it that I just need to understand how to structure the code so type train can access the methods with type carriage.
I really would apprecaite any help on this.
Thanks
For the purpose of an example, imagine that I am writing some code the that is concerned with moving and manipulating trains. Each train can hold a different number of carriages. I want the code I write to control several trains each with a different number of carriages.
Now from what I have read I would declare a carriage type and store the relevent data for that carriage within that type. That type would also have methods and functions to create carriages, move carriages, draw carriages etc.
Now here is the point I want clarification on. From what I have read I would then create a type train which would store a list of carriages that belong to that train. So every time I create a train it automatically creates carriages.
Am I right so far?
The problem I am having is that when it comes to dealing with the list of carraiges within the type Train, I always end up moving the methods that were originally in the carriage type (move, create, draw etc) into the train type as it seems difficult to access them otherwise.
So my question is this: Am I overcomplicating things and using OOP incorrectly or is it that I just need to understand how to structure the code so type train can access the methods with type carriage.
I really would apprecaite any help on this.
Thanks