#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 - ;
cout << sp._lifeBlood << endl;
}
int main()
{
Sprite sp();
fight(sp);
return ;
}
//声明为谁的友元就可以通过谁的对象访问谁的私有成员
#include <iostream>
using namespace std;
class Complex
{
friend Complex operator+(Complex &c1, Complex &c2);
public:
Complex(double r = , double i = ):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(, );
Complex c2(, );
sum = c1 + c2;
sum.dumpFormat();
return ;
}

全局函数作友元

#include <iostream>
#include <math.h>
using namespace std; class Point
{
friend float getdistance(const Point& p1, const Point& p2);
public:
Point(int x = , int y = ):_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(, );
p1.dumpFormat();
Point p2(, );
p2.dumpFormat();
cout << "dis:" << getdistance(p1, p2);
return ;
}

类成员做友元

#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 = , int y = ):_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(, );
p1.dumpFormat();
Point p2(, );
p2.dumpFormat();
ManagePoint m;
cout << "dis:" << m.getdistance(p1, p2);
return ;
}

友元类

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

#include <iostream>
#include <math.h>
using namespace std; class Point
{
friend class ManagePoint;
public:
Point(int x = , int y = ):_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(, );
p1.dumpFormat();
Point p2(, );
p2.dumpFormat();
ManagePoint m;
cout << "dis:" << m.getdistance(p1, p2);
return ;
}

随机推荐

  1. Gitlab分支保护

    问题:使用Git时,会碰到需要对某个分支进行保护,避免其他人随意push. 这里以gitlab为例,具体操作如下: 1.进入项目 2.点击左侧列表中的Settings 3.点击Protected Br ...

  2. Dubbo -- 关于 api接口调用不使用强依赖

    首先,我们都知道  Dubbo 调用api 需要提供暴露  接口,   消费端才通过 ZK 可以调用 通常我们都会使用 提供 api  jar包 的方式 使用  这样既方便又快捷 简单 只需要在spr ...

  3. 123457123456#0#-----com.twoapp.jingPinYinYu01----儿童学英语jiemei

    com.twoapp.jingPinYinYu01----儿童学英语jiemei

  4. DB2存储过程简单示例

    在这个示例中,我们将在DB2中创建一个名为DEMO1201的存储过程. 该存储过程的输入参数IN_NAME和IN_CREDITCARD,表示用户的姓名和身份证号. 该存储过程的作用是根据身份证号来新建 ...

  5. Zabbix之设置监控主机某个端口并发送邮件告警

    Zabbix可以配置监控主机的某个端口在该端口down之后触发发送告警邮件 一,添加监控项 选择主机 监控项 创建监控项 查看监控图形 二,设置触发器 设置触发器当该监控的端口down时可以发送告警 ...

  6. ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法

    这个是非常常用的案例,ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法/** * 单文件上传 * name:表单上传文件的名字 * ext: 文件允许的后缀,字符串形式 * path:文件保 ...

  7. IBM.WMQ订阅消息

    网上关于IBM这个消息队列中间件的资料相对比较少,C#相关的资料就更少了,最近因为要对接这个队列中间件,花了不少功夫去搜索.整理各种资料,遇到很多问题,因此记录下来. 1.基于 amqmdnet.dl ...

  8. 最新 梦网科技java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.梦网科技等10家互联网公司的校招Offer,因为某些自身原因最终选择了梦网科技.6.7月主要是做系统复习.项目复盘.Leet ...

  9. mysql每次update数据,自动更新对应表中时间字段

    mysql 已经创建完成表的情况下, 使得其中的时间字段 在每次 uodate 数据的时候 自动更新事件, 运行如下sql ALTER TABLE tab_name MODIFY COLUMN upd ...

  10. 5、1 es 数据库和mysql 数据库同步 (Windows操作系统)

    (1)在logstash-5.6.8安装目录下创建文件夹mysqletc (名称随意) (2)文件夹下创建mysql.conf (名称随意) ,内容如下: input { jdbc { #sc表 jd ...