Friend function Demo : Friend « Class « C++






Friend function Demo

Friend function Demo
  
#include <iostream>
using namespace std;

class myclass {
  int num;
public:
  myclass(int x) { 
     num = x; 
  }
  friend int isneg(myclass ob);
};

int isneg(myclass ob)
{
  return (ob.num < 0) ? 1 : 0;
}

int main()
{
  myclass a(-1), b(2);

  cout << isneg(a) << ' ' << isneg(b);
  cout << endl;

  return 0;
}




           
         
    
  








Related examples in the same category

1.Make the inserter into a friend functionMake the inserter into a friend function
2.Define friend function for <<Define friend function for <<
3.friend class for each other
4.Friends can access private members of a class.
5.Use friend function to access the non-public member variable