A friend function is a non-member function that is granted access to the private and protected members of a class. This means that a friend function can access private and protected data members of a class as if it were a member of that class.
Friend functions are declared inside the class definition but are defined outside the class definition. They are declared using the friend
keyword followed by the function signature, just like any other function declaration.
Friend functions are useful when you need to access the private or protected members of a class from outside the class definition, but you don't want to make those members public.
#include <iostream>
using namespace std;
class Cls{
public:
string name = "Michael";
int age = 40;
friend void friendFunction(Cls x);
};
void friendFunction(Cls x) {
cout << x.name << " " << x.age << endl;
}
int main() {
Cls Object;
friendFunction(Object);
return 0;
}
This C++ code defines a class Cls
with two public data members name
and age
. It also declares a friend function friendFunction()
that takes an object of Cls
as its argument.
The friendFunction()
function is defined outside the class definition but is declared as a friend of the Cls
class using the friend
keyword. This means that the friendFunction()
function can access the private and protected members of the Cls
class.
In the main()
function, we create an object of Cls
named Object
, and then pass it as an argument to the friendFunction()
function. The friendFunction()
function simply prints out the name
and age
data members of the Cls
object passed to it.
What's the main difference between "private" and "protected"
Private
and protected
are access specifiers that are used to define the visibility of class members.
private
members are only accessible from within the class where they are declared. They cannot be accessed from outside the class or by any derived classes. This means that they are hidden from the outside world, and can only be accessed through public member functions (methods) of the class.
protected
members are also hidden from the outside world, but can be accessed by derived classes. This means that they are accessible from the class in which they are declared and any classes derived from that class. However, they cannot be accessed by any other classes or functions outside of the class hierarchy.
The main difference between private
and protected
members is that private
members are completely hidden from the outside world, while protected
members are accessible to derived classes. This allows derived classes to access and modify the protected members of their base class, but not the private members.
In general, it is good practice to use the private
access specifier for most members, and only use protected
when it is necessary to provide access to derived classes. This helps to ensure that the internal implementation of the class is not exposed to the outside world, and that the class can maintain its invariants and encapsulation.
No comments:
Post a Comment