Mapping a real world object to OOP
-
how to map an object of the so called real life to programming world. Say there is a car. How can we take this to programming world. Car with wheel, engine, etc. Car can move without even doors, it's movement will be effected if one wheel is with low air pressure. etc. Here car should be an object, wheel also an object. Car has wheels, doors etc., but wheel has different impact than doors as far as motion is concerned. Can someone help me visualize this object to OOP?
-
how to map an object of the so called real life to programming world. Say there is a car. How can we take this to programming world. Car with wheel, engine, etc. Car can move without even doors, it's movement will be effected if one wheel is with low air pressure. etc. Here car should be an object, wheel also an object. Car has wheels, doors etc., but wheel has different impact than doors as far as motion is concerned. Can someone help me visualize this object to OOP?
You can't, not really. Every car has some stains on the seats, are you going to bother to map those ? Chips in the paint ? Oil on the engine ? No. You choose the bits that are important to what you want to write, and you map those to classes. Getting it exactly the way it is in the real world is not the point, mapping the bits you want to interact with so they work smoothly in your program, is.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
You can't, not really. Every car has some stains on the seats, are you going to bother to map those ? Chips in the paint ? Oil on the engine ? No. You choose the bits that are important to what you want to write, and you map those to classes. Getting it exactly the way it is in the real world is not the point, mapping the bits you want to interact with so they work smoothly in your program, is.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
>>You can't, not really. Help me to some extent. >>Every car has some stains on the seats, are you going to bother to map those ? Yes, anything with some volume should be able to interact with the seat. There some physics rules should come handy. and so on >> Chips in the paint ? Yes, again some interaction with the environment. How environment can effect the car? >> Oil on the engine ? Yes, of course. Help me outhere as well. >> No. You choose the bits that are important to what you want to write, and you map those to classes. Getting it exactly the way it is in the real world is not the point, mapping the bits you want to interact with so they work smoothly in your program, is. 'm want to simulate the max possible.
-
>>You can't, not really. Help me to some extent. >>Every car has some stains on the seats, are you going to bother to map those ? Yes, anything with some volume should be able to interact with the seat. There some physics rules should come handy. and so on >> Chips in the paint ? Yes, again some interaction with the environment. How environment can effect the car? >> Oil on the engine ? Yes, of course. Help me outhere as well. >> No. You choose the bits that are important to what you want to write, and you map those to classes. Getting it exactly the way it is in the real world is not the point, mapping the bits you want to interact with so they work smoothly in your program, is. 'm want to simulate the max possible.
sinanju wrote:
'm want to simulate the max possible.
Why ? Are you writing a game ? Why would you care if there's oil ON the engine ( under the hood ), or chips in the paint, or stains in the seat ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
how to map an object of the so called real life to programming world. Say there is a car. How can we take this to programming world. Car with wheel, engine, etc. Car can move without even doors, it's movement will be effected if one wheel is with low air pressure. etc. Here car should be an object, wheel also an object. Car has wheels, doors etc., but wheel has different impact than doors as far as motion is concerned. Can someone help me visualize this object to OOP?
public class Car { private Wheel [] wheels; private Door [] doors; private SteeringWheel steeringWheel; private GearBox gearBox; private Engine engine; public Car() { wheels = new Wheel[4]; doors = new Door[4]; gearBox = new GearBox(); steeringWheel = new SteeringWheel(); engine = new TwoLitreEngine(); } public void Start() { gearBox.SetNeutral(); while(!engine.Started) { engine.EngageStarterMotor(); } engine.DisengageStarterMotor(); } }
You can go on ad infinitum implementing whatever objects you want to define. The above code relies on an implementation ofWheel
,Door
,GearBox
, andSteeringWheel
. Also, a base class calledEngine
and an implementation of it calledTwoLitreEngine
is defined. -
public class Car { private Wheel [] wheels; private Door [] doors; private SteeringWheel steeringWheel; private GearBox gearBox; private Engine engine; public Car() { wheels = new Wheel[4]; doors = new Door[4]; gearBox = new GearBox(); steeringWheel = new SteeringWheel(); engine = new TwoLitreEngine(); } public void Start() { gearBox.SetNeutral(); while(!engine.Started) { engine.EngageStarterMotor(); } engine.DisengageStarterMotor(); } }
You can go on ad infinitum implementing whatever objects you want to define. The above code relies on an implementation ofWheel
,Door
,GearBox
, andSteeringWheel
. Also, a base class calledEngine
and an implementation of it calledTwoLitreEngine
is defined.Car a is a very large object to map. This class is a decent basic overview, but due to the massive variations between types of car, you could consider making it abstract and defining specific types of car. This implementation assumes that a car will have 4 wheels (some had 3), and 4 doors (some have 2), and does the book count as a door? Manual or Auto gearbox, etc. e.g. public class VauxhallCorsa : public Car { public VauxhallCorsa() { wheels = new Wheel[4]; doors = new Door[3]; gearBox = new ManualGearBox(); steeringWheel = new SteeringWheel(); engine = new OneLitreEngine(); } }
-
Car a is a very large object to map. This class is a decent basic overview, but due to the massive variations between types of car, you could consider making it abstract and defining specific types of car. This implementation assumes that a car will have 4 wheels (some had 3), and 4 doors (some have 2), and does the book count as a door? Manual or Auto gearbox, etc. e.g. public class VauxhallCorsa : public Car { public VauxhallCorsa() { wheels = new Wheel[4]; doors = new Door[3]; gearBox = new ManualGearBox(); steeringWheel = new SteeringWheel(); engine = new OneLitreEngine(); } }
wheelerbarry wrote:
This class is a decent basic overview, but due to the massive variations between types of car, you could consider making it abstract and defining specific types of car.
Considering the OP isnt even proficient in defining objects yet, I tried to stear clear of the obvious abstractions. Of course, the base class should be called
Vehicle
allowing definition of a Boat (no wheels, still with engine), Bycycle (2 wheels, no engine/gearbox) etc etc etc. My answer was simply to give a basic layout of a class, which i think is what the OP asked for!