Home Segments Index Top Previous Next

188: Mainline

You know that you can refer to a member-variable value directly by using the class member operator. In particular, you know that you can refer to the value of the radius member variable of a particular tank_car object named t:

t.radius 

Alternatively, you can refer to a member-variable value indirectly by defining a member function that returns the member-variable value. In the following tank_car class definition, for example, the addition of a definition for a member function named read_radius indicates that read_radius returns the value of the radius member variable:

class tank_car {
  public:
    double radius, length;
    tank_car ( ) {radius = 3.5; length = 40.0;}
    tank_car (double r, double l) {radius = r; length = l;}
    double read_radius ( ) {return radius;}  
    double volume ( ) {return pi * radius * radius * length;} 
}; 

Accordingly, with read_radius defined, you have another way to refer to the value of the radius member variable of a particular tank_car named t:

t.read_radius ( )