![]() |
![]() |
![]() |
![]() |
![]() |
|
Analogously, you do not need to assign a member-variable value directly.
Instead, you can assign a member-variable value indirectly by defining a
member function that does the actual value assigning. In the
following tank_car class definition, for example, the addition of
a definition for a member function named write_radius
indicates that write_radius assigns a value to 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;}
void write_radius (double r) {radius = r;}
double volume ( ) {return pi * radius * radius * length;}
};
With write_radius defined, you have another
way to assign a value to the radius member variable of a particular
tank_car object named t:
t.write_radius (4.0)
Because the only purpose of write_radius is to assign a value to a
member variable, write_radius is marked void, indicating that
no value is to be returned.