Friend Function

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

FRIEND FUNCTIONS

Khem Nath Khatiwada


Aspire College
Biratnagar
• Friend Function
• A friend function can access the private and protected members of
other class in which it is declared as friend.
• Other words,If a function is friend function of a class, that friend
function is not the actual member of the class. But which function has
rights to access to all private and protected members (variables and
functions).
#include<iostream.h> class B r= x.a + y.b;
#include<conio.h> { cout<<”The value of class A
private: object =”<<x.a<<endl;
class B; int b; cout<<”The value of class B
object =”<<y.b<<endl;
class A public:
cout<<”The sum of both values
{ B() =”<<r<<endl;
private: { }
int a; b=35; main()
public: } {
A() friend void show(A,B); A obj1;
{ }; B obj2;
a=25; show(obj1, obj2);
} void show(A x, B y) getch();
friend void show(A,B); { }
}; int r;
#include<iostream.h> friend class B; cout<<”The value of b:
#include<conio.h> }; ”<<obj.b<<endl;
class B }
class A { };
{ public:
private: B() main()
int a,b; void showA(A obj) {
public: { A x;
A() cout<<”The value of a: B y;
{ ”<<obj.a<<endl; y.showA(x);
a=10; } y.showB(x);
b=20; void showB(A obj) getch();
} { }
#include <iostream> }; void total_salary(boss b,employee e)
using namespace std; class employee {
class employee; {private: int total=b.salary+e.salary;
class boss { int salary; cout<<"total salary"<<total;
private: public: }
int salary; employee()
public: {} int main()
boss() employee(int b ) {
{} { double sboss,semployee;
boss(int a) salary=b; cout<<"enter boss salary and
{ } employee salary";
salary=a; void display_employee_salary() cin>>sboss;
} { cin>>semployee;
void display_boss_salary() cout<<"employee boss b1(sboss);
{ salary"<<salary<<endl; b1.display_boss_salary();
cout<<"boss salary"<<salary<<endl; } employee e1(semployee);
} friend void e1.display_employee_salary();
total_salary(boss,employee) ;//friend total_salary(b1,e1);
friend void function
total_salary(boss,employee) ;//friend return 0;
function };
}
• Simple Program for Friend Function Algorithm/Steps:
• STEP 1: Start the program.
• STEP 2: Declare the class name as Base with data members and
member functions.
• STEP 3: The function get() is used to read the 2 inputs from the user.
• STEP 4: Declare the friend function mean(base ob) inside the class.
• STEP 5: Outside the class to define the friend function and do the
following.
• STEP 6: Return the mean value (ob.val1+ob.val2)/2 as a float.
• STEP 7: Stop the program.
Simple Program for Friend cin >> val1>>val2; clrscr();
Function } base obj;
#include<iostream.h> friend float mean(base obj.get();
#include<conio.h> ob); cout << "\n Mean value
}; is : " << mean(obj);
class base { getch();
int val1, val2; float mean(base ob) { }
public: return float(ob.val1 + Sample Output
ob.val2) / 2; Enter two values: 10, 20
void get() { } Mean Value is: 15
cout << "Enter two
values:"; void main() {
• A standard function, not a member function of a class, has no
privilege to access private data members, but a friend function can
access any private data members. A friend function can also be used
for function overloading.
• A Friend function declaration can be specified anywhere in the class.
But it is good practice to define it where the class ends. Declaring the
friend function is straightforward. The keyword friend comes before
the class prototype inside the class definition.
• Syntax
class className {
...
friend returnType functionName(arguments);
...
}
#include <iostream> func(Distance); //friend int main()
using namespace std; function {
}; Distance D;
class Distance { cout<<"Distace:
private: int func(Distance d) "<<func(D);
int meter; {
public: //function definition return 0;
Distance() d.meter=10; //accessing }Output
private data from non- 10
{ member function
meter=0; Here, the friend function
return d.meter; func() is declared inside the
} } Distance class. So, private
data can be accessed from
friend int this function
• Characteristics of a Friend function:

• The function is not in the scope of the class to which it has been
declared as a friend.
• It cannot be called using the object as it is not in the scope of that class.
• It can be invoked like a normal function without using the object.
• It cannot access the member names directly and has to use an object
name and dot membership operator with the member name.
• It can be declared either in the private or the public part.
#include <iostream>
using namespace std;
class Box int printLength(Box b) int main()
{ { {
private: b.length += 10;
int length; return b.length; Box b;
public: } cout<<"Length of box:
Box(): length(0) { } "<< printLength(b)<<endl;
friend int
printLength(Box); //friend return 0;
function }
};
Let's see a simple example when friend void min(A,B); // if(a.x<=b.y)
the function is friendly to two friend function. std::cout << a.x << std::endl;
classes. }; else
class B std::cout << b.y << std::endl;
#include <iostream> { }
using namespace std; int y; int main()
class B; public: {
// forward declarartion. void setdata(int i) A a;
class A { B b;
{ y=i; a.setdata(10);
int x; } b.setdata(20);
public: friend void min(A,B); min(a,b);
void setdata(int i) // friend function
return 0;
{ };
}
x=i; void min(A a,B b)
} {
C++ Friend class using namespace std; {
A friend class can access cout<<"value of x is :
both private and class A "<<a.x;
protected members of }
the class in which it has {
been declared as friend. int x =5; };
friend class B; // int main()
Let's see a simple friend class. {
example of a friend class. }; A a;
class B B b;
#include <iostream> { b.display(a);
public: return 0;
void display(A &a) }

You might also like