More

    Difference Between Friend Function and Friend Class.

    Friend Function and Friend Class – Using the friend keyword, the Friend function and friend class are used to access a class’s private members. The main difference between friend function and friend class is that with friend function, private class members can be accessed, while with friend class, only the names of the friend class can be accessed, not the private members.

    Whether used by a method or a class, the friend feature may have a negative effect on the object-oriented system because it weakens encapsulation, which goes against the object-oriented paradigm. This is why the friend function must be used carefully, or the code’s data hiding will be broken.

    This function of a friend is neither commutative nor transitive. The fact that X is a friend of Y does not imply that Y is a friend of X. It does not follow that if X is a friend of Y and Y is a friend of Z that X is also a friend of Z.

    Definition of Friend FunctionThe friend function is used to access a class’s private and secure members by giving the non-member function access. When declaring this type of function, a friend keyword is used before the function name. The friend role is subject to a set of restrictions. The first prerequisite is that a child class does not inherit the friend function. The second condition is that the storage class specifier must not be present in the friend feature, preventing it from being declared static or extern.

    The friend function is not invoked with a class invoking object. A global function, a member function of a class, and a function prototype are all examples of friend functions.

    Definition of Friend Class

    We may render one class a friend of another class using the friend function, referred to as friend class, to gain access to the other class’s private members. It’s important to note that instead of inheriting different types, friend classes can only access the names specified within them. Members of the first class cannot, in reality, become members of the friend class. These friend classes aren’t used very much.

    It is possible to declare the friend class in multiple classes. It is a fast alternative method for friend functions since, instead of creating several friend functions, we can construct a friend class that can access all of the data members and functions.

    Key Differences Between Friend Function and Friend Class

    1. A friend feature has access to a class’s private and protected members. On the other hand, a friend class is a class that assists in accessing a class’s private members.
    2. The friend feature prototype is included within the class, and the keyword friend is used to precede it. Similarly, the keyword friend is used to describe a friend class.
    3. In the case of a friend function, the forward declaration is required; however, it is not needed in a friend class.

    Conclusion

    When a function needs to access two or more separate classes, internal members, a friend function is required. OnOn the other hand, a friend class is required when a class needs to access the members of another class. It is preferable to use a friend class when a multiple-member function wants to be a friend of another function.

     Read also: The Google Nest Hub Max soups up the smart display

     

     

    Facebook Comments Box

    Recent Articles

    Related Stories

    Leave a Reply

    Stay on op - Ge the daily news in your inbox

    Facebook Comments Box