Friend Functions - ordinary functions (i.e. non-member functions) that have extraordinary access to member variables
syntax : prototype is in class definitiion
prototype is preceded by keyword member
using Friend functions vs using accessors
Example from Display 83. on page 447
void Money::input(istream& ins)
{
char one_char, decimal_point, digit_1, digit_2;
long dollars;
int cents;
bool negative;
ins >> one_char;
if (one_char == '-')
{
negative = true;
ins >> one_char;
}
else
negative = false;
ins >> dollars >> decimal_point >> digit_1 >> digit_2;
if (one_char != '$') || (decimal_point != '.')
|| !isdigit(digit_1) || !isdigit(digit_2))
{
cout << "Error
- illegal form for money input\n";
exit (1);
}
cents = digit_to_int(digit_1) *10 + digit_to_int(digit_2);
all_cents = dollars * 100 + cents;
if (negative)
all_cents = -all_cents;
}
The helping function digit_to_int
int digit_to_int(ch)
{
return int(ch) - int('0');
}
Const Modifiers
const parameters
const member functions
Overloading Operators
Constructors for Automatic Type Conversion
Overloading Unary Operators
Overloading << and >>