Similarly, the first_link_pointer
and
current_link_pointer
member variables are included in the
header
class definition. You might reasonably suppose that they
should be accessible only to the member functions of the header
class:
// An interim definition of header; as given, it does not work! class header { public: header ( ) { first_link_pointer = NULL; current_link_pointer = first_link_pointer; } void add (railroad_car *new_element) { first_link_pointer = new link (new_element, first_link_pointer); current_link_pointer = first_link_pointer; } void advance ( ) { current_link_pointer = current_link_pointer -> next_link_pointer; } railroad_car* access ( ) { return current_link_pointer -> element_pointer; } int endp ( ) { return ! current_link_pointer; } void reset ( ) { current_link_pointer = first_link_pointer; } private: link *first_link_pointer; link *current_link_pointer; };
At this point, six member functionsadd
, advance
,
access
, endp
, reset
, and
header
remain generally accessible. Accordingly, as
you may recall from Chapter 14, these six functions are said to
constitute the public interface to the link
and
header
classes.
Note, however, that moving the member variables and the constructor of the
link
class to the private portion of the class definition makes them
inaccessible not only for general use, but also for the use of the member
functions in the header
class.
*-----------------------* *-------------------------* | header | | link | | | | | | *-----------------* | Access | *-------------------* | | | Public portion | | denied | | Private portion | | | | -----------------* | | | | | | | | | | | | | | *-----------------* | <---* | *-------------------* | *-----------------------* *-------------------------*