Friend function in c++

260 views 11 slides Jan 10, 2021
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

This PPT contains all information about friend function in objective oriented programming. that is need, characteristic and syntax of friend function. Although I used cpp in this ppt.


Slide Content

Shri vaishnav institute of management, indore Friend function By: Jagrati Mehra Course: B. Sc. (CS) II year Submitted to: Prof. Someshwar Joshi

Why we need friend function? Suppose there is two different objects of two different classes. class ABC ABC a int data function() { ........ } class XYZ XYZ x int data function() { ........ }

class ABC ABC a int data function() { ........ } class XYZ XYZ x int data function() { ........ } But what if I need to access both data and information ? Here we use friend function. Access not possible ..!!!

So the syntax of friend function? Simple only add friend in front of member function. We don’t need to write ( friend ) while defining out of class. void display { //body } friend void display { //body } How is friend function is different from normal function? Let understand by a program- Normal function Friend function

#include< iostream.h > #include< conio.h > class test { int data1 , data2 ; public : test () {} test ( int a , int b ) { data1 = a ; data2 = b ; } void display (); //normal function friend void multiply ( test ); //friend function };

void test :: display () //normal function in scope of class. { cout << “data 1= ” << data1 ; //directly access private variable cout << “data 2=“ << data2 << endl ; } void multiply ( test t ) //object as argument. { int data0 ; data0 = t . data1 * t.data2 ; //need object name and dot(.) cout << “multiplication =“ << data0 ; } void main () { test obj ( 100 , 200 ) ; obj . display (); //need object name and dot(.) while calling. multiply ( obj ); //direct calling. getch (); }

Characteristics of friend function friend function need not required scope resolution operator. friend function can be declared in private or public doesn’t matter. friend function can not be called using object of the class. friend function need object name and dot (.) to access private members. Usually it passes objects as arguments. The function can define anywhere like a normal function.

friend function in multiple class friend void total(boss b,employee e); class boss boss b int salary void display() { ........ } class employee employee e int salary void display() { ........ }

#include< iostream.h > #include< conio.h > class boss { int salary ; public : boss () {} boss ( int a ) { salary = a ; } void display () { cout << “Boss Salary =“ << salary ; } friend void total ( boss , employee ); }; class employee { int salary ; public : employee () {} employee ( int b ) { salary = b ; } void display () { cout << “Employee Salary =“ << salary ; } friend void total ( boss , employee ); };

void total_salary ( boss b , employee e ) { int total ; total = b . salary + e . salary ; cout << “Total Salary =“ << total << endl ; } void main () { boss b ( 50000 ); b . display (); employee e ( 25000 ); e . display (); total ( b , e ); getch (); } Boss Salary =50000 Employee Salary=25000 Total Salary = 75000

Thank You