009_linuxC++之_友元函数
(一)定义:友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。类授予它的友元特别的访问权。通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你的类时,还要征得其它部分的拥有者的同意)。
(二)使用非友元函数将两个对象中的变量进行相加
#include <iostream>
#include <string.h>
#include <unistd.h> using namespace std; class Point {
private:
int x;
int y; public:
Point() {}
Point(int x, int y) : x(x), y(y) {} int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
}; Point add(Point &p1, Point &p2)
{
Point n;
n.setX(p1.getX()+p2.getX());
n.setY(p1.getY()+p2.getY());
return n;
} int main(int argc, char **argv)
{
Point p1(, );
Point p2(, ); Point sum = add(p1, p2);
sum.printInfo(); return ;
}
(三)使用友元函数进行两个对象中变量的相加
#include <iostream>
#include <string.h>
#include <unistd.h> using namespace std; class Point {
private:
int x;
int y; public:
Point() {}
Point(int x, int y) : x(x), y(y) {} int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point &p1, Point &p2);
}; Point add(Point &p1, Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
} int main(int argc, char **argv)
{
Point p1(, );
Point p2(, ); Point sum = add(p1, p2);
sum.printInfo(); return ;
}
009_linuxC++之_友元函数的更多相关文章
- C++_友元函数
1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 具体来说:为了使其他类的成员函数直接访问该类的私有变量 即:允许外面的类或函数去访问类的私有变量和保护 ...
- C++_友元函数总结(转)
原文地址:http://blog.csdn.net/insistgogo/article/details/6608672 1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 ...
- C++_友元函数(转)
1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 具体来说:为了使其他类的成员函数直接访问该类的私有变量 即:允许外面的类或函数去访问类的私有变量和保护变量,从而使两个类共享同 ...
- String类型_static成员_动态内存分配_拷贝构造函数_const关键字_友元函数与友元类
1:String类型 #include <iostream> using namespace std; int main() { //初始化方法 string s1 = "hel ...
- C++的友元类和友元函数实例
#include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...
- c++友元函数
c++友元函数分两类: 一://友员全居函数 /*#include <iostream>using namespace std;class aaa{ friend void prin ...
- 重载运算符:类成员函数or友元函数
类成员函数: bool operator ==(const point &a)const { return x==a.x; } 友元函数: friend bool operator ==(co ...
- 不可或缺 Windows Native (20) - C++: 友元函数, 友元类
[源码下载] 不可或缺 Windows Native (20) - C++: 友元函数, 友元类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演 ...
- [Reprint]C++友元函数与拷贝构造函数详解
这篇文章主要介绍了C++友元函数与拷贝构造函数,需要的朋友可以参考下 一.友元函数 1.友元函数概述: (1)友元函数是定义在一个类外的普通函数.友元函数和普通函数的定义一样;在类内必须将该普通函 ...
随机推荐
- go if 便捷语句
之前使用java C#没这么用过. 绝对新技能 if v := math.Pow(x, n); v < lim { 跟 for 一样,`if` 语句可以在条件之前执行一个简单的语句. 由这个语 ...
- 【Manacher】Colorful String
The value of a string s is equal to the number of different letters which appear in this string. You ...
- 画一个秘密花园 | Scratch 3.0 艺术项目
项目类型:艺术 难度指数:3.5颗星 适合年龄:9岁以上 角色个数:1 程序个数:1 学习时长:建议60min——90min 项目内容: 点击绿色旗子,音乐声响起.随后在舞台上点击一下,就出现一朵花, ...
- [Es6]原生Promise的使用方法
参考:https://www.cnblogs.com/imwtr/p/5916793.html 1.new Promise(func) 通过实例化构造函数成一个promise对象,构造函数中有个函数参 ...
- Java 实现简单的 RPC 框架
RPC 简介 RPC,全称为 Remote Procedure Call,即远程过程调用,它是一个计算机通信协议.它允许像调用本地服务一样调用远程服务.它可以有不同的实现方式,而不需要了解底层网络技术 ...
- SOLID Principles
Intention: more understandable, easier to maintain and easier to extend.(通过良好的设计使得代码easy and simple, ...
- Form key length limit 2048 exceeded ,提交数据时,数据的键过长 或者是上传文件过大
在ASP.NET Core MVC中,文件的key 默认最大为2048,文件上传的最大上传文件默认为20MB,如果我们想上传一些比较大的文件,就不知道怎么去设置了,没有了Web.Config我们应该如 ...
- VBA宏注释(四)
注释用于记录程序逻辑和用户信息,其他程序员将来可以阅读并理解相同的代码无缝工作. 它包括由开发者,修改者以及还可以包括合并逻辑的信息. 解释器在执行时忽略注释. VBA中的注释用两种方法表示,它们分别 ...
- java计算接口调用时间
方法一: LocalDateTime beginTime = LocalDateTime.now(); Long opetime = Duration.between(between,LocalDat ...
- vue、react中循环遍历为什么会有key,key有什么作用?
先讲一下,vue和react都是在操作虚拟dom,并且根据diff算法进行新旧dom对比,从而更新dom,以vue举例: vue官方文档中写到有 key 的特殊属性主要用在 Vue 的虚拟 DOM 算 ...