Forum

Friend Function

This thread contains 1 reply, has 2 voices, and was last updated by Test 5 years, 10 months ago.

  • Author Replies
    • #52985

      What is a friend function How do we use it?


    • #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


Viewing 1 reply thread

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

Subscribe to get latest updates regarding courses, IT industry and career prospects.

Copyright 2016-2024 Smart Mentors. All Rights Reserved.

Login

FORGOT PASSWORD

Sign up now to Become An Instructor

    Register your Interest

      cf7captchaRegenerate Captcha