Information Technology Reference
In-Depth Information
Accessing Members from Inside the Class
As mentioned before, members of a class can access the other class members by just using
their names.
For example, the following class declaration shows the methods of the class accessing the
fields and other methods. Even though the fields and two of the methods are declared private ,
all the members of a class can be accessed by any method (or any function member) of the
class. The code is illustrated in Figure 4-7.
class DaysTemp
{
// Fields
private int High = 75;
private int Low = 45;
// Methods
private int GetHigh()
{
return High; // Access private field
}
private int GetLow()
{
return Low; // Access private field
}
public float Average ()
{
return (GetHigh() + GetLow()) / 2; // Access private methods
}
} Accessing the private methods
Figure 4-7. Members within a class can freely access each other.
Search WWH ::




Custom Search