Forum

Friend Function

This thread contains 1 reply, has 2 voices, and was last updated by  Test 7 months, 2 weeks ago.

  • Author Replies
  • #52985

    What is a friend function How do we use it?


  •  Test
    #52987

    Friend function goes about as a friend of the class. It can get to the private and protected members from the class. The friend function isn’t a member from the class, however, it must be recorded in the class definition.

    The non-member function can’t get to the private data of the class. At times, it is essential for the non-member function to get to the data.

    The friend function is a non-member function and can get to the private data of the class.

    Following are the qualities of a friend function:

    1) The friend function isn’t to the extent of the class in which it has been declared.

    2) Since it isn’t to the extent of the class, so it can’t be called by utilizing the object of the class. Consequently, friend function can be invoked like a normal function.

    3) A friend function can’t get to the private members directly, it needs to utilize an object name and dot operator with every member name.

    4) Friend function utilizes objects as contentions.

    Let’s understand this through an example:

    #include <iostream>
    using namespace std;
    class Addition
    {
    int a=4;
    int b=6;
    public:
    friend int add(Addition x)
    {
    return(x.a+x.b);
    }
    };
    int main()
    {
    int result;
    Addition x;
    result=add(x);
    cout<<result;
    return 0;
    }

    Output: 10


You must be to reply to this thread.Please or . Registration is 100% free.

GET FREE CONSULTATION

Call Us Now For Free Consultation97370 05566

Our experts listen to you patiently and suggest you the right course after conducting a personality profile test. Register your interest below to schedule personality profile test for you.

SUBSCRIBE TO OUR NEWSLETTER

Contact Us

Working Hours

  • Monday9:00 AM – 9:00 PM
  • Tuesday9:00 AM – 9:00 PM
  • Wednesday9:00 AM – 9:00 PM
  • Thursday9:00 AM – 9:00 PM
  • Friday9:00 AM – 9:00 PM
  • Saturday9:00 AM – 9:00 PM
  • SundayClosed
Copyright 2016-2019 Smart Mentors. All Rights Reserved.

Login

FORGOT PASSWORD

Sign up now to Become An Instructor

Register your Interest