KEMBAR78
Friend Function | PDF | Parameter (Computer Programming) | Scope (Computer Science)
0% found this document useful (0 votes)
8 views16 pages

Friend Function

Uploaded by

kashaffff786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views16 pages

Friend Function

Uploaded by

kashaffff786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Friend Function

Friend Function

•The concept of data hiding and encapsulation dictates that


a non member function will not be able to access any
private or protected data of object.
•But there may arise situation(s) where private and
protected members need to be accessed by non member
functions.
•The above can be done by a special function called friend
function.

Friend Function Slide Number 2


Friend Function …

•A friend function is a non member having full access rights


to every private or protected members of a class.

Friend Function Slide Number 3


Characteristics of Friend Function

•It is not in the scope of the class, to which it has been


declared as friend.
•As it is not in the scope of the class, it cannot be called
using the object of that class.
•It can be invoked / called like a normal function.

Friend Function Slide Number 4


Characteristics of Friend Function …

•It can be declared either in the private or public part of a


class.
•The objects act as its argument.

Friend Function Slide Number 5


How to define Friend Function?

•Definition of the friend function is just like that of any


other normal C++ function, follows just after the class
definition of which it is a friend.
•The general syntax of friend function is “friend return_type
function_name(argument list);”, where the keyword
“friend” is used as function modifier.

Friend Function Slide Number 6


Friend Function: The Example is

class student
{
int roll;
char name[20];
int marks[5];

Friend Function Slide Number 7


Friend Function: The Example is

public:
void read();
void write();
friend int cal(student x);
};

Friend Function Slide Number 8


Friend Function: The Example is

int cal()
{
int msum=0;
for(int i=0;i<5;i++)
{
msum=msum+marks[i];

Friend Function Slide Number 9


Friend Function: The Example is

}
return msum;
}

Friend Function Slide Number 10


Friend Function: The Example is

void student::read()
{
cout<<“enter the roll”;
cin>>roll;
cout<<endl<<“enter the name”;
gets(name);
cout<<endl<<“enter marks for 5 different subjects”;

Friend Function Slide Number 11


Friend Function: The Example is

cout<<endl<<“enter marks for 5 different subjects”;


for(int a=0;a<5;a++)
{
cin>>marks[i];
}
}

Friend Function Slide Number 12


Friend Function: The Example is

void student::write()
{
cout<<“The roll is:”<<roll;
cout<<endl<<“The name is:”;
puts(name);
cout<<endl<<“The marks for 5 different subjects
are:”;

Friend Function Slide Number 13


Friend Function: The Example is

for(int a=0;a<5;a++)
{
cout>>marks[i];
}
}

Friend Function Slide Number 14


Friend Function: The Example is

int main()
{
clrscr();
student s;
s.read();
s.write();

Friend Function Slide Number 15


Friend Function: The Example is

cout<<cal(s);
getch();
return 0;
}

Friend Function Slide Number 16

You might also like