This thread is getting nuts. These examples are academic and quite impractical.
You guys are trying to create a class that is too complicated for what should be incredibly simple. We are talking about 2 different things here: Cartesian coordinates and polar coordinates. Yes, you will likely want a way to convert between the two... but that does
not mean they should be in the same class. They are two very different things and have different capabilities.
Encapsulating this class just so you can combine polar/cartesian under the same roof is a bad approach. To use the class effectively, the user is going to need to know which kind point they are dealing with. IE, if the user is moving points around linearly a lot, they're going to want the implementation to be cartesian. Likewise I'm sure there's some kind of operation that works better with polar coordinates, but I never use them so I can't think of one. =P
Let the programmer choose their tool. Don't try to make an all-in-one.
The programmer can choose between vector/map/list/deque. Yes, they all do more or less the same thing, but their implementation is different. Some are better at some operations than others. There's no general 'container' class that tries to guess which underlying storage mechanism would be best. That'd be ridiculous. Just as there shouldn't be a general 'point' class which tries to guess whether or not polar/cartesian storage would be best.
Not every class needs to be encapsulated. It's not always a good idea. Here is one such instance where encapsulation does not help, but rather just obfuscates the code and makes the class much clunkier to use.
MiiniPaa wrote: |
---|
Many questions abous "Should I use getters/setters" are void if you design classes beginning with public interface first. |
I agree. And this kind of goes with what I was trying to say originally: if your idea of encapsulation is just writing getters/setters for all your members, you are not encapsulating. To do it properly, you have to think of the interface first, and the implementation afterwards.
It should be immutable (aside from assigment). So no mutators at all. |
I'm not a fan of the immutable paradigm. It makes classes harder to use.
LB wrote: |
---|
@Disch your record class is well designed. |
Ugh. It's crap.
You could make the functions virtual and have MemoryRecord, DiskRecord, DatabaseRecord, etc. |
There's no value to that. Are MemoryRecords and DiskRecords going to have a different "name" attribute? If not, then why would get/setName need to be virtual? And if yes, then get/setName is a bad function anyway because it's ambiguous. Which name are we getting/setting?