Showing posts with label friend. Show all posts
Showing posts with label friend. Show all posts

Thursday, October 21, 2010

Friendship isn't inherited, transitive, or reciprocal

Just because I grant you friendship access to me doesn't automatically grant your kids access to me, doesn't automatically grant your friends access to me, and doesn't automatically grant me access to you.
  • I don't necessarily trust the kids of my friends. The privileges of friendship aren't inherited. Derived classes of a friend aren't necessarily friends. If class Fred declares that class Base is a friend, classes derived from Base don't have any automatic special access rights to Fred objects.
  • I don't necessarily trust the friends of my friends. The privileges of friendship aren't transitive. A friend of a friend isn't necessarily a friend. If class Fred declares class Wilma as a friend, and class Wilma declares class Betty as a friend, class Betty doesn't necessarily have any special access rights to Fred objects.
  • You don't necessarily trust me simply because I declare you my friend. The privileges of friendship aren't reciprocal. If class Fred declares that class Wilma is a friend, Wilma objects have special access to Fred objects but Fred objects do not automatically have special access to Wilma objects.

Overloading < And >

Without friend
int operator > (const Complex& right) const { return (real > right.getReal() && img > right.getImg(); }



Using Friend
friend int operator < (const Complex& left, const Complex& right){----//TODO}

Wednesday, September 1, 2010

Using friend to overload operators

class loc {
  int longitude, latitude;
public:
  loc() {}
  loc(int lg, int lt) {
    longitude = lg;
    latitude = lt;
  }
   
  void show() {
    cout << longitude << " ";
    cout << latitude << "\n";
  }
   
  loc operator=(loc op2);
  friend loc operator++(loc &op);
  friend loc operator--(loc &op);
};

loc operator++(loc &op)
{
  op.longitude++;
  op.latitude++;
   
  return op;
}
   
// Make op-- a friend; use reference.
loc operator--(loc &op)
{
  op.longitude--;
  op.latitude--;
   
  return op;
}