Accessing Components of an Object

If the components are not otherwise protected, they can be accessed by using the form

reference.field

For example:

class Quantity
{
double multiplier;
StringList numerator;
StringList denominator;
....  constructors and methods for Quantity ....
}

 

Suppose we have declarations:

 

Quantity Q, R;

 

Access the various fields by:

Q.multiplier
R.numerator

etc.

This will work whether we are using the field's value or assigning to it.

However, this is not the preferred way of using objects.

Next Slide Previous Slide Contents