next up previous contents
Next: Constructors Up: Some basics of object-oriented Previous: Classes and minimal memory   Contents

Encapsulation

In C++, one typically encapsulates variables. This does not have a major advantage at the level of this text, but we do it to conform with the standard. It goes as follows:


{}
class Node {
private:
    int id_ ; // Convention: I add underscores to private variables.
    double xCoord_ ;
    double yCoord_ ;
public:
    void set_id( int tmp ) { id_ = tmp ; }
    int id() { return id_ ; }
    void set_x( double tmp ) { xCoord_ = tmp ; }
    double x() { return xCoord_ ; }
    ...
} ;
...
Node* node ;
...
node = new Node() ;
...
node->set_id( 213 ) ;
xx = node->x() ;
private: means that everything in that block can only be accessed by methods which are defined inside the class definition, i.e. inside the class Node block.



2004-02-02