Friend Fun

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

Friend Function

&
Friend Class
Friend Function
• The private members can not be accessed
from outside the class.

• A non-member function can not have an


access to the private data of a class.

• However ……. ?
Friend Function continue…

• C++ allows a common function to be made


friendly with more than one classes, thereby
allowing the function to have access to the
private data of these classes.
• Such a function need not be a member of
these classes.
• To make an outside function friendly to a
class, we have to simply declare this function
as a friend of the class.
Friend Function continue…

• The function class employee


declaration should be {
preceded by the
keyword friend. ---
• The function is defined ---
elsewhere in the public :
program like a normal ---
C++ function. ---
• The function definition friend void it_cal
does not use either the (void);
keyword friend or the
scope operator : :. }
Friend Function continue…

• The functions that are declared with the


keyword friend are known as friend function.
• A function can be declared as a friend in any
number of classes.
• A friend function, although not a member
function, has full access right to the private
members of the class.
Friend Function continue…

Special Characteristics:
• It is not in the scope of the class to which it
has been declared as friend.

• Since it is not in the scope of the class, it


cannot be called using the object of the class.

• It can be invoked like a normal function


without the help of any object.
Friend Function continue…

Special Characteristics:
• Unlike member functions, it cannot access
the member names directly and has to use an
object name and dot membership operator
with each member name.
• It can be declared either in the public or
private part of a class without affecting its
meaning.
• Usually, it has objects as arguments.
Friend Function continue…

• Member function of one class can be friend


functions of another class.

• In such cases, they are defined using the


scope resolution operator as:
Friend Functions continue…

class X class Y
{ {
… …
… …
int fun1 ( ); friend int X : : fun1 ( );
… …
}; };
Friend Class continue…

We can also declare all class Z


the member functions of {
one class as the friend …
functions of another class.

friend class X ;
In such cases, the class is
called a friend class. …
};
• class Temperature • int main()
• {
• int celsius;
• {
• public: • Temperature tm;
• Temperature()
• { • cout <<
• celsius "Temperature in
= 0;
• }
celsius : " << temp( tm )
• friend int temp( Temperature f ); << endl;
• // declaring friend function • return 0;
• };
• • }
• int temp( Temperature t )
• // friend function definition
• {
• t.celsius = 40;
• return t.celsius;
• }

• class B; • class B
• class A • {
• { • int value;
• int value; • public:
• public: • B()
• A() • {
• { • value = 3;
• value = 5; • }
• } • friend int sum(A,
• friend int sum(A, B);
B); • };
• };
• int main()
• int sum( A a, B b )
• {
• {
• A a;
• return (a.value +
b.value); • B b;
• } • cout << "Sum : "
<< sum( a, b ) << endl;
• return 0;
• }
• class Rectangle
• class Square • {
• { • int length,int breadth;
• friend class Rectangle; • public:
• int getArea()
• int side; • {
• public: • return length * breadth;
• Square ( int s ) • }
• { • void shape( Square obj )
• side = s; • {
• } • length = obj.side;
• }; • breadth = obj.side;
• }};

• int main()
• {
• Square obj(5);
• Rectangle
rectangle;

rectangle.shape(obj);
• cout <<
rectangle.getArea() ;
• return 0;
• }

You might also like