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

C++ - Example of Friend Function - 1

Category: Notes

Course: B.Tech.

Semester: 4

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 member function of "complx" class. We are using it as Friend function in "Eminem" class .
class Eminem ;
class complx
{
      int alpha, beta ;

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

int main( )
{
      complx C1 ;
      Eminem object ;
      
      C1.show( object ) ;        //Show is member function of "complx" class, that's why, C1.show , not object.show.

      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 friend function of both the classes ----> https://passhojao.com/notes/c-example-of-friend-function-2