You are on page 1of 6

OOP Course Al-Balqa

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).

2. A function or an entire class may be declared to be a friend of another class.

3. Friendship declarations can be placed anywhere in the class definition.


a) A good programming practice is to place all friendship declarations
first in the class immediately after the class header and to not precede
them with any member-access specifier.

4. To declare a function as a friend of a class, precede the function prototype in


the class definition with the keyword friend.
a) To declare class ClassTwo as a friend of class ClassOne, place a
declaration of the following form in the definition of ClassOne.

friend ClassTwo; // friend class ClassTwo; is also OK

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

8. It is possible to specify overloaded functions as friends of a class. Each


overloaded function intended to be a friend must be explicitly declared in the
class definition as a friend of the class.

2
OOP Course Al-Balqa
Applied Unviversity

FRIEND FUNCTIONS AND FRIEND CLASSES (CONTINUED)

9. Example of friend functions, friend member functions, and friend classes:

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

CONVERSION OF DIGITS TO AN INTEGER

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.

2. The definition of the function is:

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.

3. The function digit_to_int is called in the program in order to compute the


number of cents. The call has the format:

cents = digit_to_int(digit1) * 10 + digit_to_int(digit2)

where digit1 is the "ten's" position and digit2 is the "one's" position.

4. Another method to convert characters to digits is to call the compiler


provided function atoi().
a) The prototype for atoi() is located in stdlib.h.
b) atoi() converts a string (a sequence of characters terminated by \0) to
an integer value.
i) atoi() returns the converted value or zero if the string cannot be
converted.
c) Syntax is:

int atoi(const char *s);

4
OOP Course Al-Balqa
Applied Unviversity

CONVERSION OF DIGITS TO AN INTEGER (CONTINUED)

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

THE CONST PARAMETER MODIFIER

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.

3. Example of both usages of const:

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.

You might also like