Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Friend Function in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 146
    Comment on it

    Friend Function:- Friend Function is Friendly to all the classes which can be declared as a friend of that class. "Friendship is given not taken" this statement means to say the friend is always declared by the class it self. when any class can use friend keyword in front of any non member function that function can access all it's private data member as well as member functions. The below code will show you the implementation of friend keyword.

    #include<iostream.h>
    #include<conio.h>
    
    class A
    {
    private:
    char str[100];
    
    public:
    void setName();
    friend void display(A);
    
    };
    
    void A::setName()
    {
        cout<<"Enter the Name:=";
        cin>>str;
    }
    
    void display(A a)
    {
        cout<<"Name is:="<<a.str;
    }
    void main()
    {
    A a;
    clrscr();
    
    a.setName();
    
    display(a);
    getch();
    }
    

    In above code display function is not a member function of Class A but Class A can be declared display function as friend using friend keyword in front of that function. So display function can be access all the Private things of Class A.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: