0% found this document useful (0 votes)
73 views2 pages

Any Data Which Is Declared Under Private Visibility Mode Inside

- A class can declare non-member functions and other classes as "friends", allowing them access to the class's private data. - For friend functions, once declared as a friend it can access private data of the class. For friend classes, the friend class can access private data of the class that declared it as a friend. - Examples are provided demonstrating how to declare friend functions and classes to allow access to private data from outside the class.

Uploaded by

Naik N.A.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views2 pages

Any Data Which Is Declared Under Private Visibility Mode Inside

- A class can declare non-member functions and other classes as "friends", allowing them access to the class's private data. - For friend functions, once declared as a friend it can access private data of the class. For friend classes, the friend class can access private data of the class that declared it as a friend. - Examples are provided demonstrating how to declare friend functions and classes to allow access to private data from outside the class.

Uploaded by

Naik N.A.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

FRIEND

Any data which is declared under private visibility mode inside a class is not accessible from outside the
class. A function which is not a member or an class can never access such private data. But there may be
some cases, where a programmer will need to access to the private data from non-member functions and
external classes. C++ offers some new mechanism in such cases.

A class can allow non-member functions and other classes to access its own private data, by making
them as friends. This documentation give two important points.

• Once a non-member function is declared as a friend, it can access the private data of the class
• similarly when a class is declared as a friend, the friend class can have access to the private data
of the class which made this a friend

Let's see a sample in this C++ tutorial for each of the above cases.

Friend function sample:

#include <iostream.h>
int Friend1(int x);
class friend_tutorial {
int data;
friend int Friend1(int x);
public:
friend_tutorial()
{
data = 5;
}
};
int Friend1(int x)
{
friend_tutorial F1;
return var1. data + x;
}
int main()
{
cout << "Added Result is "<< friend(4)<<endl;
}

The output of the above C++ Tutorial sample will be


Added Result is 9

Friend class:

Declaration of a friend class is also similar. Only thing is a class definition is slightly different.

Friend function:

#include < iostream.h >


class CPP1
{
int data;
friend class friendclass;
public:
CPP1()
{
data = 5;
}
};
class friendclass
{
public:
int deletefrom(int x)
{
CPP1l var2;
return var2.data - x;
}
};
int main()
{
friendclass var3;
cout << "Added Result is: "<< var3.deletefrom(2);
}

The output of the above C++ Tutorial sample will be


Subtracted Result is: 3

This is a good way out given by C++ to avoid restrictions on private variables. But this should be used
with caution though. If all the functions and classes are declared as friends, then the concept of
encapsulation and data security will go for a toss.

That is why the concept of friend functions and classes should be used with proper judgment.

You might also like