A5-Friend Functions and Friend Classes
A5-Friend Functions and Friend Classes
3. Concept Map
Before going to the syntax of friend functions it is important that we highlight why they are needed. Friend functions
provide a relaxing mechanism through which we can access the private data members of a class directly without any
question being asked. This does not mean that we have relaxed the access specifiers. A friend function provides access
to the private and public data members of a class from only within its body. Friend functions are needed because
sometimes if we have multiple classes and we want to manipulate the class members through a single function.
Another similar scenario is when we want a regular function (that is not member of a class) to access private members
of a class. The final scenario is the use of friend functions in operator overloading (will be discussed in operator
overloading lecture).
Always remember that friend functions are not members of a class they are just regular functions with special
privileges. In order to make a friend function you have to specify that a particular function is a friend function. This
can be achieved through the following syntax:
Page 36
Friend Functions and Friend Classes
private:
int member1;
int membern;
public:
friend void fun( first , second ); //Friend function prototype
};
class second
private:
int mem1;
int mem2;
public:
friend void fun( first, second ); //Friend function prototype
};
void fun( first o1, second o2) //Note that friend is not written
cout<<o2.mem1<<o2.mem2;
void main( )
first obj1;
second obj2;
fun( obj1,obj2); //Simple calling. Cannot use the dot operator
In the syntax above it must be understood that access specifiers are not applicable on friend functions i.e. whether you
write the function in public or private it has no effect. Another important thing to always remember is that it is
compulsory to write the keyword friend with the function prototype but writing the friend keyword in the function
definition will result in a syntax error. In the above code the friend function is bridge between two classes using a
single function. Creating this environment is purely the programmers choice. It is not necessary that you have a
function working like a bridge. You can always have a single friend function inside a single class.
When discussing friend functions one must remember that friend functions are never part of a class. They are written
in the class to show that they have a friendship with the class. Friend functions are not called using the dot notation or
by using the scope resolution. Friend functions are called like a conventional function. Now when using the friend
function we can access both public and private data members directly as if there is no such thing as an access specifier.
An important note regarding friend functions is that these functions should not be used to relax the access specifiers.
Secondly the friendship of a function is one sided i.e. the function can access the class members but the class cannot
access deceleration that are made inside the function.
Page 37
Friend Functions and Friend Classes
class first
private:
friend class second; //Declaration of friend class
int member1;
int membern;
public:
first()
member1=10;
member2=20;
};
class second
private:
int mem1;
int mem2;
public:
void fun( first o1) //Friend function prototype
cout<<mem1<<mem2;
};
void main( )
first obj1;
second obj2;
obj2.fun( obj1); //cannot call using obj1! Obj1 does not contain a
function called fun
Page 38
Friend Functions and Friend Classes
5.1. Tools
Visual Studio 2012.
class series
public:
series(int f, int n, int e)
first=f;
nterm=n;
class sum
public:
void series_sum(series &obj)
};
Page 39
Friend Functions and Friend Classes
int main()
{
series obj1(6,96,31); //(first term, last term, total number of terms)
sum obj2;
obj2.series_sum(obj1);
display(obj1); //Call the friend function
system ("pause");
return 0;
}
Figure 1: The series class and the sum friend class. The display function is a friend function.
5.5. Compilation
After writing the code, compile your code according to the guidelines mentioned. Remove any errors and warnings
that are present in your code.
6. Practice Tasks
Your task is to create a class named equation which will have the data members a, b and c which are the coefficients
of the quadratic equation. The class will have two more data members namely proot and nroot which stand for the
positive root and negative root of the equation. Suppose that variables a, b and c are integers. Where proot and nroot
Page 40
Friend Functions and Friend Classes
are floats.
Page 41
Friend Functions and Friend Classes
• Construct the class objects by using a nullary constructor.
• Then design a friend function which will determine the proot and nroot of the equation.
• Create another friend function which will display the values of proot and nroot.
6.4. Outcomes
After completing this lab, students will be able explain the difference between a member function and a friend
function. The students will be able to create friend functions and friend classes. Further they will be comfortable with
the manipulation of objects using both friend functions and classes.
6.5. Testing
Test Cases for Practice Task-1
Sample Inputs Sample Outputs
a =1; proot= 1
b =2; nroot = -3
c =-3
Page 42
Friend Functions and Friend Classes
7. Further Reading
7.1. Books
• Object-Oriented Programming Using C++, Fourth edition, Joyce Farrell
7.2. Slides
• The slides and reading material will be provided by course instructor.
Page 43