Download Our Beta Android App And Help Us Build Awesome Stuff!  Download Now.

C++ - Example of Friend Function - 2

Category: Notes

Course: B.Tech.

Semester: 4

Friend Function strikes back !
THIS IS NOT ANY SORT OF ASSIGNMENT OR LAB WORK(yet) .
It's just so that you can Understand and Learn. \m/

#include <iostream>
using namespace std ;

//Here show is just a regular function. It is not part of any of the class.
//Show is a Friend function of both the classes.

class Eminem ;
class complx
{
      int alpha, beta ;

      public:
            complx( )
            {
                  alpha = beta = 10 ;
            }
            friend void show( complx, Eminem ) ;
};
class Eminem
{
      int raps ;
      public:
            Eminem( )
            {
                   raps = 17 ;
            }
            friend void show( complx, Eminem ) ;
};
void show( complx C1, Eminem object )
{
      cout<<"Complex number is : "<<C1.alpha<<" + i"<<C1.beta<<endl<<endl ;
      cout<<"Data of Raps(not Rape) : "<<object.raps <<endl ;
}

int main( )
{
      complx C1 ;
      Eminem object ;

      show( C1, object ) ;        //Show kisika part nhi hai , so object ke through call nhi kiya.

      return 0 ;
}

P.S. I will be uploading more such codes on the topics being discussed in class. :D

Added by StAn

Comments
Posted by StAn,

When show() is a friend function of one class and member function of another class ----> https://passhojao.com/notes/c-example-of-friend-function-1