Professional Documents
Culture Documents
Applied Unviversity
FRIEND FUNCTIONS
AND FRIEND CLASSES
1. A friend function of a class is a function defined outside that class and has the
right to access private and protected members of the class (as well as public
members).
5. Friendship is:
a) Granted, not taken. For class B to be a friend of class A, class A must
declare that class B is its friend.
b) Neither symmetric nor transitive. If class A is a friend of class B, and
class B is a friend of class C, you cannot infer that:
i) Class B is a friend of class A (not symmetric).
ii) Class A is a friend of class C (not transitive).
6. Member access notions of private, protected, and public are not relevant to
friendship declarations, so friendship declarations can be placed anywhere in
the class definition.
7. Friend functions and classes should only be used as needed. The concept of
friendship is a violation of encapsulation.
1
OOP Course Al-Balqa
Applied Unviversity
2
OOP Course Al-Balqa
Applied Unviversity
class tweedledee {
friend void alice(); // friend function
...
public:
int cheshire(); // member function
...
};
class tweedledum {
friend int tweedledee::cheshire(); // friend member function
...
public:
...
};
class tweedledumber {
friend class tweedledee; // friend class -- all
// member functions of class
// tweedledee have access
...
public:
...
};
void alice()
{
...
}
3
OOP Course Al-Balqa
Applied Unviversity
1. The money program in display 8.3 (pp. 443-448 and p. 450) of the Savitch
textbook contains a function digit_to_int which converts single digits (char
types) to a one-place integer value.
int digit_to_int(char c)
{
return ( int(c) - int('0') );
}
a) For example, if the char parameter c is '3', this equates to the ascii
value of 51. The ascii value of '0' is 48. Thus 51 - 48 = 3, i.e. the
integer value.
where digit1 is the "ten's" position and digit2 is the "one's" position.
4
OOP Course Al-Balqa
Applied Unviversity
d) Example:
#include <iostream.h>
#include <stdlib.h>
int main()
{
char buffer[128];
int result;
cout << "Enter integer value to convert: ";
cin.getline(buffer, 128); // getline() is a function that
// reads characters (including whitespace)
// until a newline character is detected
// or, in this case, 127 characters have
// been read.
result = atoi(buffer);
cout << "Result is " << result << endl;
return 0;
}
e) atoi() can be used to convert digits entered as the type character to the
type integer.
i) atoi() will create an integer out of the digits as it reads them from
left to right.
a) If a non-digit character is embedded in the string of
characters, atoi() will convert the leading digits up to but
not including the character and whatever follows the
character.
b) If the leading character is not a digit, atoi() will return 0.
5
OOP Course Al-Balqa
Applied Unviversity
1. If you place the modifier const before the type for a call-by-reference
parameter, the parameter is called a constant parameter.
a) const informs the compiler that this parameter should not be changed
within the function.
b) Parameters of a class type that are not changed by the function should
be constant call-by-reference parameters, rather than call-by-value
parameters because, in most cases, it is more efficient to pass by
reference than by value.
2. If the member function does not change the value of its calling object (i.e. the
implied instance), then you should code the const modifier immediately
following the parameter list.
a) By coding const, the compiler will flag as an error any function that
attempts to modify the implied instance's member variables.
class Sample {
public:
Sample();
int compare(const Sample& s) const;
void input();
void output() const;
private:
int stuff;
double more_stuff;
};
4. By using const:
a) You direct the compiler to flag any errors where an implied instance
and/or parameter is modified in the function.
b) You indicate to anyone reading your code that the function was not
supposed to modify the implied instance and/or parameter.