Friend Function and
Friend Class
Friend Functions
and Classes
In C++ , one cannot access private/protected members of a class from another function outside the class or from another class. This rule does not apply to friend functions and classes. This can be accomplished by declaring functions and classes with friend keyword.
In C++ , one cannot access private/protected members of a class from another function outside the class or from another class. This rule does not apply to friend functions and classes. This can be accomplished by declaring functions and classes with friend keyword.
Objective
- Understanding Friend
Functions and Classes
- To know the need for Friend
Functions and Classes
- To know the advantages and
disadvantages of using Friend Functions and Classes
What is a friend
function?
A function which is not a member of the class can access the private and protected members of the class if it is declared a friend of that class. This friend function must be declared within the class with the keyword friend preceding it.
A function which is not a member of the class can access the private and protected members of the class if it is declared a friend of that class. This friend function must be declared within the class with the keyword friend preceding it.
Need for friend
functions
The friend function can be used to access the private members of a class as if its a member function of the class.
The friend function can be used to access the private members of a class as if its a member function of the class.
Advantage of friend
functions
Instead of calling member functions like objectName.function( ) , friend functions can be called as function( ) , so it is improves readability of the code.
Instead of calling member functions like objectName.function( ) , friend functions can be called as function( ) , so it is improves readability of the code.
Disadvantages of
friend functions
Dynamic binding is very difficult with friend functions , because non member functions can not be declared virtual .
Dynamic binding is very difficult with friend functions , because non member functions can not be declared virtual .
Example Program for
friend functions
#include
<iostream>
using namespace std;
using namespace std;
//Must be known to
TWO
//before declaration of ONE.
class ONE;
//before declaration of ONE.
class ONE;
class TWO
{
public:
void print(ONE& x);
};
{
public:
void print(ONE& x);
};
class ONE
{
int a, b;
friend void TWO::print(ONE& x);
public:
ONE() : a(1), b(2) { }
};
{
int a, b;
friend void TWO::print(ONE& x);
public:
ONE() : a(1), b(2) { }
};
void
TWO::print(ONE& x)
{
cout << "a is " << x.a << endl;
cout << "b is " << x.b << endl;
}
{
cout << "a is " << x.a << endl;
cout << "b is " << x.b << endl;
}
int main()
{
ONE xobj;
TWO yobj;
yobj.print(xobj);
}
{
ONE xobj;
TWO yobj;
yobj.print(xobj);
}
Output:
a is 1
b is 2
b is 2
What is a friend
class?
A friend class is a
class which can access the private and protected members of another class. The
friend class must be defined first , then within the definition of the other
class , class should be made as a friend to the class by using the keyword
friend.
Need for Friend
class
The friend class can be used to access the private memebers of a class with which friendship is made.
The friend class can be used to access the private memebers of a class with which friendship is made.
Advantage of Friend
class
Sharing of private members between classes is possible by using friend class.
Sharing of private members between classes is possible by using friend class.
Example Program
#include
<iostream>
using namespace std;
using namespace std;
class MyClass
{
// Declare a friend class
friend class SecondClass;
{
// Declare a friend class
friend class SecondClass;
public:
MyClass() : Secret(0){}
void printMember()
{
cout << Secret << endl;
}
private:
int Secret;
};
MyClass() : Secret(0){}
void printMember()
{
cout << Secret << endl;
}
private:
int Secret;
};
class SecondClass
{
public:
void change( MyClass& yourclass, int x )
{
yourclass.Secret = x;
}
};
{
public:
void change( MyClass& yourclass, int x )
{
yourclass.Secret = x;
}
};
void main()
{
MyClass my_class;
SecondClass sec_class;
my_class.printMember();
sec_class.change( my_class, 5 );
my_class.printMember();
}
{
MyClass my_class;
SecondClass sec_class;
my_class.printMember();
sec_class.change( my_class, 5 );
my_class.printMember();
}
Output:
05
05
Reviewed by NEERAJ SRIVASTAVA
on
10:13:00 PM
Rating:
No comments: