Concept of friend function in c++

Can we write member function of one class as friend of another class ? How


6 Answers
1-6 of  6
6 Answers
  • hello am saying that...c+ friend funtion.advertiments.a friend funtion of class defined outside the class but access the all right  protecd member access of the class friend funtion appeer the class deftion.
    thank you

  • A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
    A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.
    By using the keyword friend compiler knows the given function is a friend function.     
    For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend.

  • Private class. K members ko hm outside the class access kr sakte h

  • A C++ friend functions are special functions which can access the private members of a class. They are considered to be a loophole in the Object Oriented Programming concepts, but logical use of them can make them useful in certain cases.
    For instance: when it is not possible to implement some function, without making private members accessible in them. This situation arises mostly in case of operator overloading.
    In the following example, the friend function print is a member of class TWO and accesses the private data members a and b of class ONE.
    syntax:-
    #include <iostream>
    using namespace std;
     
    //Must be known to TWO
    //before declaration of ONE.
    class ONE;
     
    class TWO
    {
    public:
    void print(ONE& x);
    };
     
    class ONE
    {
    int a, b;
    friend void TWO::print(ONE& x);
    public:
    ONE() : a(1), b(2) { }
    };
     
    void TWO::print(ONE& x)
    {
    cout << "a is " << x.a << endl;
    cout << "b is " << x.b << endl;
    }
     
    int main()
    {
    ONE xobj;
    TWO yobj;
    yobj.print(xobj);
    }
     
     

  • Yes, we can write member function of one class as friend of another class.These can be one of the important uses/needs of friend function in c++.

  • Yes,we can write member function of one class as friend of another class.
     
    #include <iostream.h>
     
    class B;
     
    class A
    {
    public:
        void showB(B& );
    };
     
    class B
    {
    private:
        int b;
    public:
        B()  {  b = 0; }
        friend void A::showB(B& x); // Friend function
    };
     
    void A::showB(B &x)
    {
        // Since show() is friend of B, it can
        // access private members of B
        std::cout << "B = " << x.b+5;
    }
     
    int main()
    {
        A a;
        B x;
        a.showB(x);
        return 0;
    }

    THE OUTPUT WILL BE
    B=5

Object Oriented Programming

Didn't get the answer.
Contact people of Talent-Object Oriented Programming directly by clicking here