I am getting started on C++ and self educating myself from a text book. I am kind of confused at one particular point in the introduction to access specifiers.
the author starts off saying private members cannot be accessed directly by objects of a class and they can only be accessed by member functions. I am copy pasting the example he used in the book.
Code:
class clockType
{
public:
bool equalTime(const clockType& otherClock) const;
//Function to compare the two times
//Postcondition: Returns true if this time is equal to
// otherClock; otherwise, returns false
private:
int hr; //stores the hours
int min; //store the minutes
int sec; //store the seconds
};
bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
In this example, the equalTime() function is using otherClock.hr, otherClock.min etc which is a direct access to private variables of the class object otherClock. I couldn't quite understand his explanation for this. I understand it is from inside a member function but it is another object's private variable directly being accessed here. Can anyone please clarify.