friend

 

#include <iostream>

using namespace std;
//friend 友元,效率的问题
//get 方法和set方法,是标准封装的结果,friend破坏了这种封装。但又带来效率的提高。但又带来了效率的提高。
//有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类的数据成员,这时可以将这些函数定义为该类的友元函数
class Sprite
{
   friend void fight(Sprite& sp);
   public:
      Sprite(int lb):_lifeBlood(lb){}
      int getLifeblood()
      {
         return _lifeBlood;
      }
      void setLifeblood(int lb)
      {
         _lifeBlood = lb;
      }
   private:
      int _lifeBlood;
};

void fight(Sprite& sp)
{
   // sp.setLifeblood(sp.getLifeblood() - 20);
   // cout << sp.getLifeblood();
   sp._lifeBlood = sp._lifeBlood - 20;
   cout << sp._lifeBlood << endl;
}
int main()
{
   Sprite sp(100);
   fight(sp);
   return 0;
}

 

//声明为谁的友元就可以通过谁的对象访问谁的私有成员
#include <iostream>
using namespace std;
class Complex
{
   friend Complex operator+(Complex &c1, Complex &c2);
public:
   Complex(double r = 0, double i = 0):real(r), image(i){}
   void dumpFormat()
   {
      cout << "(" << real << "," << image << ")";
   }
private:
  double real;
  double image;
};

Complex operator+(Complex &c1, Complex &c2)
{
   Complex c;
   c.real = c1.real + c2.real;
   c.image = c1.image + c2.image;
   return c;
}


int main()
{
   Complex sum;
   Complex c1(1, 2);
   Complex c2(2, 4);
   sum = c1 + c2;
   sum.dumpFormat();
   return 0;
}

 

全局函数作友元

#include <iostream>
#include <math.h>
using namespace std;

class Point
{
   friend float getdistance(const Point& p1, const Point& p2);
public:
   Point(int x = 0, int y = 0):_x(x), _y(y){}
   void dumpFormat()
   {
      cout << "_x" << _x << "_y" << _y;
   }
private:
   float _x;
   float _y;

};
float getdistance(const Point& p1, const Point& p2)
{
   float dx = p1._x - p2._x;
   float dy = p1._y - p2._y;
   return sqrt(dx * dx + dy * dy);
}
int main()
{
   Point p1(3, 4);
   p1.dumpFormat();
   Point p2(7, 8);
   p2.dumpFormat();
   cout << "dis:" << getdistance(p1, p2);
   return 0;
}

类成员做友元

#include <iostream>
#include <math.h>
using namespace std;
class Point;//前向声明的问题,前向声明是一种不完全类型的声明,不能定义对象,可以定义指针和引用做参数和返回值,仅用在函数声明
class ManagePoint
{
   public:
   float getdistance(const Point &p1, const Point &p2);
};
class Point
{
   friend float ManagePoint::getdistance(const Point& p1, const Point& p2);
public:
   Point(int x = 0, int y = 0):_x(x), _y(y){}
   void dumpFormat()
   {
      cout << "_x" << _x << "_y" << _y;
   }
private:
   float _x;
   float _y;

};

float ManagePoint::getdistance(const Point &p1, const Point &p2)
{
   float dx = p1._x - p2._x;
   float dy = p1._y - p2._y;
   return sqrt(dx * dx + dy * dy);
}
int main()
{
   Point p1(3, 4);
   p1.dumpFormat();
   Point p2(7, 8);
   p2.dumpFormat();
   ManagePoint m;
   cout << "dis:" << m.getdistance(p1, p2);
   return 0;
}

友元类

实际工作中程序员喜欢友元类

#include <iostream>
#include <math.h>
using namespace std;

class Point
{
   friend class ManagePoint;
public:
   Point(int x = 0, int y = 0):_x(x), _y(y){}
   void dumpFormat()
   {
      cout << "_x" << _x << "_y" << _y;
   }
private:
   float _x;
   float _y;

};
class ManagePoint
{
   public:
   float getdistance(const Point &p1, const Point &p2);
};
float ManagePoint::getdistance(const Point &p1, const Point &p2)
{
   float dx = p1._x - p2._x;
   float dy = p1._y - p2._y;
   return sqrt(dx * dx + dy * dy);
}
int main()
{
   Point p1(3, 4);
   p1.dumpFormat();
   Point p2(7, 8);
   p2.dumpFormat();
   ManagePoint m;
   cout << "dis:" << m.getdistance(p1, p2);
   return 0;
}

 

转载于:https://www.cnblogs.com/aelite/p/10852541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值