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 - ;
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 ;
}
随机推荐
- MySQL VARCHAR字段最大长度到底是多少
MySQL VARCHAR字段最大长度到底是多少 varchar(n),n表示什么? MySQL5.0.3之前varchar(n)这里的n表示字节数 MySQL5.0.3之后varchar(n)这 ...
- react起步——从零开始编写react项目
# index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- java获取中文汉字的所有拼音
java获取中文汉字的所有拼音 中文汉字可能有很多读音,java中分别用1,2,3,4来区别,例如“作”字,就有三个读音,zuo1,zuo2,zuo4. java获取汉字读音拼音代码如下所示: S ...
- delphi两个取字串长度的函数strlen,length
━━━━━━━━━━━━━━━━━━━━━━━━━━ [strlen]PChar 串专用函数:串长度 procedure TForm1.BitBtn1Click(Sender: TObject); v ...
- CRISP-DM
CRISP-DM (cross-industry standard process for data mining), 即为"跨行业数据挖掘标准流程". 此KDD过程模型于1999 ...
- 【leetcode_easy】590. N-ary Tree Postorder Traversal
problem 590. N-ary Tree Postorder Traversal 参考 1. Leetcode_easy_590. N-ary Tree Postorder Traversal; ...
- laravel的workflow流程插件
原文地址:https://github.com/brexis/laravel-workflow
- 经典PID控制及应用体会总结
经典PID控制及应用体会总结 PID控制原理 PID是一种线性控制器,它根据给定值rin(t)与实际输出值yout(t)构成控制方案: 重点关注相关算法是如何对偏差进行处理的: PID控制器各校正环节 ...
- mysql 记录 - concat、concat_ws、group_concat 的用法
本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字 ...
- zookeeper 操作命令
简介 查阅了网上相关资料,介绍zookeeper客户端命令并不是非常全面,大多数都是简单介绍ls.get.set.delete.stat这几个简单命令的,下面我把help中的所有命令简单介绍一下以供参 ...