#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. spring bean容器加载后执行初始化处理@PostConstruct

    先说业务场景,我在系统启动后想要维护一个List常驻内存,因为我可能经常需要查询它,但它很少更新,而且数据量不大,明显符合缓存的特质,但我又不像引入第三方缓存.现在的问题是,该List的内容是从数据库 ...

  2. Spring事务的隔离级别和传播机制

    七个传播机制:(红色字体的代表如果不设置传播机制时候默认的)PROPAGATION_REQUIRED-支持当前事务;如果不存在,创建一个新的. PROPAGATION_SUPPORTS-支持当前事务; ...

  3. (九)UML之活动图

    一.概念 二. 在Rational rose 中画活动图 2.1 创建Activity Diagram 2.2 画图

  4. EasyNetQ使用(九)【非泛型的发布&订阅扩展方法,发生错误的情况 】

    自从EasyNetQ第一个版本开始,它就可以发布/订阅特定类型的消息. bus.Subscribe<MyMessage>("subscriptionId", x =&g ...

  5. 线程.Qt更新界面

    1.信号&槽 机制 即可 ZC:个人暂时 测试下来,类似是 PostMessage(...)的方式: a.是在各自的线程中执行代码, b.调用 emit不耗时(指的意思是 像调用PostMes ...

  6. CenOS 7 防火墙 端口 systemctl 操作项

    Centos开放端口 # firewall-cmd --zone=public --add-port=3306/tcp --permanent Centos关闭端口 # firewall-cmd -- ...

  7. java中创建线程的方式

    创建线程的方式: 继承thread 实现runnable 线程池 FurureTask/Callable 第一种:继承thread demo1: public class demo1 { public ...

  8. laydate年份选择,关闭底框,点击指定年份就选择然后关闭控件,翻页不选择也不关闭控件

    如下图,翻页不选择也不关闭.点击指定年份时再选择和关闭控件 代码如下 // 默认没有选择,把判断赋值当前时间 var iYearCode = parseInt(new Date().getFullYe ...

  9. Linux系统目录的学习

    1.在公司中linux 都是没有界面 2.系统路径    2.1 /表示根目录    2.2 ~表示/root    2.3etc:存放系统配置文件    2.4 home  除了root 以外所有用 ...

  10. 如何使用js实现轮播图

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...