Adapted from: Encapsulation is not information hiding
The bundling of data with the methods that operate on that data
Hide the internal representation, or state, of an object from the outside
struct
with fields only and no access specifiersclass
Integrate data with methodspublic
, private
, protected
Access Type | Visibility |
---|---|
public |
All code |
protected |
class A methodsfriend functions of class A class derived from class A |
private |
class A methodsfriend functions s of class A |
private
whenever possibleconst
whenever possible for reference and pointer parameters and methodslatitude
and longitude
, in the Position classdistance()
and heading()
, are free functions in a separate fileheading()
and calculate the opposite of what is neededPosition
are public and directly accessible. It is possible to store invalid latitude and longitude values, producing garbage distance and headings (GIGO)Position
require changes in the implementation of distance()
and heading()
(∆ Position.hpp ⟶ ∆ PathCalculations.cpp)Place data and the operations that perform on that data in the same class
Position.hpp
heading()
Don't expose data items
const
off of the methodconst
Don't expose the difference between stored data and derived data
speed()
, getSpeed()
std::vector
getPositions()
has to change, and the client code has to changeauto
in the client code, and perhaps a typedef
can help, but not entirely prevent thissetPosition()
exposes direct design detail that we are using an indexable container, e.g., std::array
, std::vector
, and std::deque
, with an indexable iteratorstd::list
, std::forward_list
, with a bidirectional iteratorgetPosition()
is expected to be a O(1) or constant-time algorithmstd::list
, this is not truebegin()
and end()
would allow use in range-for statementsegment
is problematic. Perhaps allow iteration through segments and not positions