Data Encapsulation
List of: Discussion Topic
Subjects: C++ Interface
Contents: Fundamental Concepts

A class provides not only data storage, but also methods (or member functions) that can access and manipulate that data. The concept of having private data and public methods for accessing data is common to ACIS classes. This is how ACIS uses C++ classes to provide data encapsulation and modularization. This permits reorganization of the internals of any component while maintaining the interface.

Although it is possible to make the class data members accessible to anyone (public), it is much more common to hide the data members by making them private. Access to private class data members is restricted to the public class methods. Not all class methods need to be exposed, so some methods are also private.

For example, consider the SPAposition class, which is used to define a position in space. The declaration for this class is shown in Example 2-1. The internal data structure for this is an array of three real numbers and it has class methods for the x, y, and z components of the position: x, y, and z. At some later date, if the data structure for SPAposition were to use three separate real numbers instead of an array, this could be implemented and still be transparent to the users of this class, because the users can only access the public x, y, and z methods and not the private data elements directly.

Note ACIS globally defines the keyword real to be equivalent to a C++double or float data type using a #define statement.

C++ Example

class DECL_KERN SPAposition {

real coord[ 3 ]; // x, y and z coordinates of the position

public:


// Force creation of all positions to be by constructor.

SPAposition() {}


// Construct a position from three doubles.

SPAposition( double xi, double yi, double zi ) {


coord[ 0 ] = (real) xi;


coord[ 1 ] = (real) yi;


coord[ 2 ] = (real) zi;

}


// Construct a position from an array of three doubles.

SPAposition( double p[ 3 ] ) {


coord[ 0 ] = (real) p[ 0 ];


coord[ 1 ] = (real) p[ 1 ];


coord[ 2 ] = (real) p[ 2 ];

}


// Copy a position

SPAposition( SPAposition const &p ) {


coord[ 0 ] = p.coord[ 0 ];


coord[ 1 ] = p.coord[ 1 ];


coord[ 2 ] = p.coord[ 2 ];

}


// Extract a coordinate value.

double x() const { return coord[ 0 ]; }

double y() const { return coord[ 1 ]; }

double z() const { return coord[ 2 ]; }


double coordinate( int i ) const { return coord[ i ]; }


// Extract a coordinate value for update.

double &x() { return coord[ 0 ]; }

double &y() { return coord[ 1 ]; }

double &z() { return coord[ 2 ]; }


double &coordinate( int i ) { return coord[ i ]; }


// Set coordinate values.

void set_x( double new_x ) { coord[ 0 ] = new_x; }

void set_y( double new_y ) { coord[ 1 ] = new_y; }

void set_z( double new_z ) { coord[ 2 ] = new_z; }


void set_coordinate( int i, double new_c )

void set_coordinate( int i, double new_c )

{ coord[ i ] = new_c; }


// Position operators

// Get displacement, i.e. a vector, as difference of

// two positions.

friend DECL_KERN SPAvector operator-( SPAposition const &,


SPAposition const & );


// Translate a position by a vector.

friend DECL_KERN SPAposition operator+( SPAposition const &,


SPAvector const & );

friend DECL_KERN SPAposition operator+( SPAvector const &,


SPAposition const & );

SPAposition const &operator+=( SPAvector const & );

friend DECL_KERN SPAposition operator-( SPAposition const &,


SPAvector const & );

SPAposition const &operator-=( SPAvector const & );

...


// Transform a position.

friend DECL_KERN SPAposition operator*( SPAmatrix const &,


SPAposition const & );

friend DECL_KERN SPAposition operator*( SPAposition const &,


SPAmatrix const & );

SPAposition const &operator*=( SPAmatrix const & );


friend DECL_KERN SPAposition operator*( SPAposition const &,


SPAtransf const & );

friend DECL_KERN SPAposition operator*( SPAposition const &p,


SPAtransf const *t );

SPAposition const &operator*=( SPAtransf const & );

...


// Output details of a position

void debug( FILE * = debug_file_ptr ) const;

};


Example 2-1. Declaration of SPAposition Class

Example 2-2 shows a code fragment of two attempts to use the SPAposition class. As the declaration of the SPAposition class in Example 2-1 shows, this has private data which is an array of three real numbers, called coord. Attempting to access the private data member coord directly fails at compile time. However, accessing the public methods, such as x, is valid and will compile.

C++ Example

// Declare an instance of the class.
SPAposition my_p1(10, 15, 20);

// The compile returns an error in next statement because
// "coord" is the private data member of this class.
double my_x = my_p1.coord[0];

// The correct way to access the x coordinate of the position.
double my_x = my_p1.x();

Example 2-2. Accessing SPAposition Class Data
PDF/FCG/02CPLUS.PDF
HTM/DATA/ACIS/FCG/02CPLUS/0001.HTM