Lecture 4 Prepared by: WAQAR ISMAIL
Typical • The Friend function and friend class are the
Definition says techniques used to access the private members
of a class by using friend keyword.
that..
Data Hiding Theory
• The private data of a class can’t be accessed from the outside of the class.
• But consider the following situation,There are two classes manager and
scientist.
• We would like to use income_tax() function to operate on the objects of
these classes.
• In such situation C++ allows the common function to be made friendly with
both the classes.
Class Properties
• By default :
• functions and data of a class are private to that class
• Only the public members are accessible outside the class
• Protected members can be inherited along with public members
• No such condition where private members can be accessed from outside
the class
BUT why
• But sometimes this restriction may force programmer to write long and
complex codes. So, there is mechanism built in C++ programming to access
private or protected data from non-member functions.
Therefore
• This is done using a friend function or/and a friend class.
• So Friend Functions and Friend Classes may be used where our application
requires the access all the members of a class
YOU KNW THAT ……
• One of the important concepts of OOP is data hiding, i.e., a nonmember
function cannot access an object's private or protected data.
• The friend feature whether used by function or class can produce a
negative impact on the object-oriented framework as it weakens
the encapsulation which is against the object-oriented paradigm.
• This is the reason the friend feature must be used wisely otherwise it
could break the data hiding of the code.
• This friend feature is neither commutative and
nor transitive. Friend of
• X is a friend of Y doesn’t infer that Y is also a Friend is Not
friend of X. If X is a friend of Y and Y is a friend
of Z, does not implicate that X is a friend of Z Friend
Friend Function in C++
• A friend function of a class is defined outside that class' scope but it has
the right to access all private and protected members of the class.
• If a function is defined as a friend function then, the private and
protected data of a class can be accessed using the function.
• The complier knows a given function is a friend function by the use of
the keyword friend.
Properties
• It is not in the scope of the class to which it has been declared as friend.•
• It can’t be called using the object of that class. Thus has to be invoked like a
normal C++ function.•
• It can be declared either in the public or the private part of a class without
affecting its meaning.•
• Usually it takes objects as arguments.
Features of Friend Function
• Not a member of the class
• Invoked like normal function without any object
• Full access to private and protected members Of the class
• But can use the members for one or more specific objects
• Called without the use dot operator(does not need to be qualified with
object’s name)
How to Declare
• Include its prototype in the class , preceding it with keyword friend
• Syntax: friend ret_type func_name(arguments);
• Can be declared anywhere (in public, protected or private section) in the
class
• May have no arguments
• Objects of the class or their pointers can be passed as arguments to the
friend function
Declaration of friend function in C++
class class_name
{
... .. ...
friend return_type function_name(argument/s);
... .. ...
}
After Deceleration
Now, you can define the friend function as a normal function to access the
data of the class. No friend keyword is used in the definition.
The function declaration should be preceded by the keyword friend.
The function is defined elsewhere in the program.
The function definition does not use either the keyword friend or the scope
resolution operator ::
class className
{
... .. ...
friend return_type functionName(argument/s);
... .. ...
};
return_type functionName(argument/s) Template
{
... .. ...
// Private and protected data of className can be
accessed from
// this function because it is a friend function of
className.
... .. ...
}
#include <iostream>
using namespace std;
class Temperature
{
int celsius;
public:
Temperature()
{
celsius = 0;
};
}
friend int temp( Temperature ); // declaring friend function Example
int temp( Temperature t ) // friend function definition Code
{
t.celsius = 40;
return t.celsius;
}
int main()
{
Temperature tm;
cout << "Temperature in celsius : " << temp( tm ) << endl;
return 0;
}
Using member function of one class as friend of
other class
• We can also make a function of one class as a friend of another class.
• We do this in the same way as we make a function as a friend of a class.
• The only difference is that we need to write class_name :: in the
declaration before the name of that function in the class whose friend it is
being declared.
• The friend function is only specified in the class and its entire body is
declared outside the class
How to declare
• class A; // forward declaration of A needed by B
• class B
• {
• display( A a ); //only specified. Body is not declared
• };
• class A
• {
• friend void B::display( A );
• };
• void B::display(A a) //declaration here
• {
• }
Using member
function of one
class as friend
of other class
#include <iostream>
using namespace std;
class A; // forward declaration of A needed by B
class B
{
public:
void display(A obj); //no body declared
};
class A
{
int x;
public:
A()
{
Example
}
x = 4;
friend void B::display(A);
Code
};
void B::display(A obj)
{
cout << obj.x << endl;
}
int main()
{
A a;
B b;
b.display(a);
return 0;
}
FRIEND FUNCTION OF TWO CLASSES
• You can definitely operate on two objects of different classes without using
the friend function but the program will be long, complex and hard to
understand.
• A friend function can be friendly to 2 or more classes. The friend function
does not belong to any class, so it can be used to access private data of two
or more classes
• #include <iostream>
• using namespace std;// forward declaration
• class B;
• class A {
•
•
private:
int numA; Example:
Addition of
• public:
• A(): numA(12) { }// friend function declaration
• friend int add(A, B);};
• class B {
• private:
members of
•
•
int numB;
public:
two different
•
•
B(): numB(1) { }// friend function declaration
friend int add(A , B);};// Function add() is the friend function of classes A and
B// that accesses the member variables numA and numB
classes using
• int add(A objectA, B objectB){
• return (objectA.numA + objectB.numB);}
friend
• int main(){
• A objectA;
Function
• B objectB;
• cout<<"Sum: "<< add(objectA, objectB);
• return 0;}
friend Class in C++ Programming
• Similar to friend function we can make one class to be a friend of
another class which is referred to as friend class. So that the friend class
can gain access to private members defined within the other class.
How to declare
... .. ...
class B;
class A
{
// class B is a friend class of class A
friend class B;
... .. ...
};
class B
{
... .. ...
};
• #include <iostream>
• class A {
•
•
•
private:
public:
int a;
Example code of
•
•
• };
A() { a = 0; }
friend class B; // Friend Class
Two friend
Classes(1)
• class B {
• private:
• int b;
• public:
• void showA(A& x)
• {
• // Since B is friend of A, it can access
• // private members of A
• std::cout << "A::a=" << x.a;
• }
• };
• int main()
• {
• A a;
• B b;
• b.showA(a);
• return 0;
• }
• When a class is made a friend class, all the member functions of that class
becomes friend functions.
• In this program, all member functions of class B will be friend functions of
class A. Thus, any member function of class B can access the private and
protected data of class A. But, member functions of class A cannot access
the data of class B.
The End