Programmingoneonone is known for sharing best programming knowledge.

friend function in c++ programming

in this post, you will learn about the friend function in c++ programming and learn how to declare define a friend function in c++ programming.


friend function in c++ programming, learn c++ programming
learn c++ programming 

friend function:

to access the private data from outside the class and to use the data of one another class we use the friend function in the program. 


define a friend function inside the class:

To define a friend function inside the class you just need to add the friend function at the beginning of the function definition.

Example:
              class test
             {

                   friend void add(void); // define a friend function name add.
             };


declare a friend function:

to declare a friend function outside the class we just need to simply declare the function name and its code. Example:
in the above example, we define a friend function called add(). to declare the add() function from outside the class we need to simply declare the function like this.

                   void add()
                   {
                     code for execution;  
                   }

Note: we don't need to use the scope resolution operator or the class name. 


call the friend function:

 to call the friend function we just simply call the function with using the object. 

Example:
in the above example we declare and define the add() function, and to call the add() function we simply call the function.

Example:
               add();

Note: we don't need to call the friend function using the class's object.


An example program using a friend function.

    #include<iostream>

    class A; // declare a class 

    class B // define a class
    {
          int x;

          public:

             void scanval(int b)
             {
                 x=b;
             }

             void printval(void)
             {
               std::cout<<"\n value of x "<< x;
             }

             friend void add(A,B); // define a friend function.
    };

    class A // declare a class 
    {
       int y;

       public:

             void scanval(int a)
             {
                  y=a;
             }

             void printval(void)
             {
                  std::cout<<"\n vlaue of y "<<y;
             }

             friend void add(A,B);

    }

    // declare a friend function without using class name and scope resolution operator.
    void add( A obj1, B obj2)
    {
            std::cout<<"\n Addition of A and B "<< obj1.x + obj2.y;
    }

    int main()
    {
       A a;
       B b;

       a.scanval(10);
       b.scanval(20);
       a.printval();
       b.printval();

       add(a,b);

       return 0;
    }

Note: in above example, we declare the two class A and B. and in both classes, we define the friend function and we declare the friend function outside the class and pass the object 1 and object 2 of the class A and class B. 

"Please share this knowledge as much possible as you can and also comment me your queries and questions related to this topic. I will really grateful to help you."

No comments:

Post a Comment