Pitfall of OOP? Or me just having a hard time following good practice ...

Started by
46 comments, last by jag_oes 22 years, 3 months ago
There is something about OOP that I do not entirely understand ... I know for a fact that there must be a better way so hopefully someone can tell me. If I create a Vector class I have no problem keeping all of the properties private and adding accessor functions for every property. But, what if I create another class that has Vectors for properties ... say a Body class that as a Vector for its velocity. Then wouldn''t I have to write functions for the Body class that change the properties of the Vector? I can''t access the vector directly so i can''t invoke the accessor functions. What do I do? Thanks
Advertisement
there are wonderful features called inheritance, polymorphisism, friend classes etc, i dont know what any of them do, but im sure there useful, and ive had too many glasses of chapagne this new year
In addition to having functions to just get every property, you canadd functions to set every property. That way, external classes and functions can set the variables of the object, but the function can be defined to prevent errors.

Take a class where all the members are public, for example. Anything can access those members and change them. In your vector class (I'm not sure whether you mean mathematical vector or like an STL vector, but I'll assume STL vector), a you might have a variable that holds the amount of variables to hold memory for. If another part of the program changed that variable to, say, a negative number, there would be memory leaks all over the place. If that variable were protected by a function that changes it, you could check for improper values for that variable within the function and not change the variable at all, preventing future errors. Probably not the best example, but I hope you understand now.

Edit: Now that I re-read your question, I think I answered the wrong thing. Oh well.

No, HTML is not an OO language.

Edited by - masonium on December 31, 2001 7:46:00 PM
// Body header:class Body{  ...  Vector &SetVelocity(vector_type x,              vector_type y,              vector_type z);private:  Vector vecVelocity;  ...};Vector &Body::SetVelocity(vector_type x, vector_type y, vector_type z){  vector vecOld = vecVelocity;  vecVelocity = Vector(x,y,z);  return vecOld;}.// somewhere else:Body *bod = new Body();bod->SetVelocity(x, y, z); // <- There''s no way around this step 

Deal with it.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Deal with it? That is all? Does anyone see the major problem with this? I am positive someone has, so there must be a way around it or a better way to do things. If I create a class and I write 3 functions for "getting" and "setting" the values of the private variables then I have to (basically) rewrite those functions in every class I use the first class in. If I don''t have access to the object how can I have access to its functions? Then what if I have a class object within a class object and finally within another class object ... that could mean tons and tons of "getting" and "setting" functions that have already be written.

Thanks everyone for the help so far, but this is obviously discouraging to see ...
If you don''t like making getter/setter functions for the members, then just declare them as ''public''. Then you can access their functions directly. It''s up to you.
It depends on how far you extend your privates... er I know that sounds wrong, but...

Anyway, let''s say you declare your Vector class, use private properties and then write ''set'' and ''get'' functions. If you then put an instance of this class into another class (lets say Body), depending on whether or not this Vector variable is declared private, you may or may not need to write another set of access functions. If you declare this[/]i variable private, then you will need to write access functions in order for outsiders to access the variable. But the Body class can still access the variable directly, yet it must use the access functions of Vector since Vector is the only class that can actually directly reference the data.
I would add method(s) that make something happen - having a SetVelocity abstracts nothing, and therefore does no work.

Body::ApplyForce(vector vForce_N)
Body::ImpartImpulse(vector vImpluse_Ns)
Body::InterpolatePosition(float fElapsedTime_sec, vector* pvPosition_m);
Body::InterpolateVelocityPosition(float fElapsedTime_sec, vector* pvVelocity_mps, vector* pvPosition_m);

Body::IncreaseMass(float fAdditionalMass_kg);
Body::ReduceMass(float fAdditionalMass_kg);


I also can''t fathom a single item of a 3D vector that would need to be private nor protected.


Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
strtok,
I don''t mind making "getting" and "setting" functions ... I *want* to follow good programming practice. But, I see some major flaws with following it, and I am sure others have too, so I would like to know how to overcome it.

Zipster,
Thank you very much for that ... you are making some sense. It seems that now it is a matter of using names in context. I would need an accessor function for the body, but instead of "reset_vector" it is "reset_velocity." That still does not make me happy ...

Magmai Kai Holmlor,
A vector was just an example ... what if I then use a body object in a bigger class ... say Simulation_World. I would then lose all access to those "ApplyForce" and whatever else functions since the Body would be private. Therefore I would have to rewrite those functions for the Simulation_World class. That seems like a waste of time ...

Thank you all for the replies ... it is all good information for me to take in ...
quote:Original post by jag_oes
I don''t mind making "getting" and "setting" functions ... I *want* to follow good programming practice.

That''s a contradiction, imnsho.

quote:Original post by jag_oes
A vector was just an example ... what if I then use a body object in a bigger class ... say Simulation_World. I would then lose all access to those "ApplyForce" and whatever else functions since the Body would be private. Therefore I would have to rewrite those functions for the Simulation_World class. That seems like a waste of time ...

Well, with a simulated world you would need a way to figure out _what body you''re dealing with.

Simlation_World:ick(point2D MouseClick, Body* pBodyClicked);
And once you have the body you clicked on, you can call ApplyForce on it...

Ug, don''t inherit Body from Simulation_World, SW should have some sort of tree of them.


Start off only using public inheritence - when you inherit (publicly) you''re saying that the new object IS the old object, and every method the was public in the old object applies to the new object. If that''s _not the case the design is flawed (sometimes you accept the flaw and press-on...).


Private inheritence means it''s implemented in terms of something else - you do this just so you don''t have to copy&paste code or redefine all the methods.

Protected inheritence has uses but requires an involved example.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement