友元函数,成员函数,普通函数的区别
2016-06-09 08:20
295 查看
#include <iostream>
using namespace std;
class Time
{
public:
Time(double xx,double yy):x(xx),y(yy){}
double getx(){return x;}
double gety(){return y;}
void aaa1(Time &t);
friend void aaa2(Time &t1,Time &t2);
private:
double x;
double y;
};
void Time::aaa1(Time &t)
{
cout<<"("<<(x+t.x)/2<<","<<(y+t.y)/2<<")"<<endl;
}
void aaa2(Time &t1,Time &t2)//友元函数不有“Time::”
{
cout<<"("<<(t1.x+t2.x)/2<<","<<(t1.y+t2.y)/2<<")"<<endl;
}
void aaa3(Time &t1,Time &t2)
{
cout<<"("<<(t1.getx()+t2.getx())/2<<","<<(t1.gety()+t2.gety())/2<<")"<<endl;
}
int main()
{
Time b1(1.5,2.6),b2(5.2 ,6.4);
b1.aaa1(b2);//成员函数直接访问私有数据
aaa2(b1,b2);//友元函数也可以直接访问私有函数
aaa3(b1,b2);//普通函数只能通过外部接口
return 0;
}
using namespace std;
class Time
{
public:
Time(double xx,double yy):x(xx),y(yy){}
double getx(){return x;}
double gety(){return y;}
void aaa1(Time &t);
friend void aaa2(Time &t1,Time &t2);
private:
double x;
double y;
};
void Time::aaa1(Time &t)
{
cout<<"("<<(x+t.x)/2<<","<<(y+t.y)/2<<")"<<endl;
}
void aaa2(Time &t1,Time &t2)//友元函数不有“Time::”
{
cout<<"("<<(t1.x+t2.x)/2<<","<<(t1.y+t2.y)/2<<")"<<endl;
}
void aaa3(Time &t1,Time &t2)
{
cout<<"("<<(t1.getx()+t2.getx())/2<<","<<(t1.gety()+t2.gety())/2<<")"<<endl;
}
int main()
{
Time b1(1.5,2.6),b2(5.2 ,6.4);
b1.aaa1(b2);//成员函数直接访问私有数据
aaa2(b1,b2);//友元函数也可以直接访问私有函数
aaa3(b1,b2);//普通函数只能通过外部接口
return 0;
}
相关文章推荐
- Qt代码段1
- 第十三周实践项目3-形状类族的中的纯虚函数
- 第十四周阅读程序1
- PS学习笔记
- 第十三周上机实践项目1(1):分数类中的运算符重载
- java中子类继承父类调用利用继承方法时调用this
- 第十五周 6.6 --- 6.12
- (版本定制)第17课:Spark Streaming资源动态申请和动态控制消费速率原理剖析
- 用Supervisord管理Python进程
- 浅谈java中的多线程及synchronized锁重入的含义
- eclipse创建android项目,无法正常预览布局文件
- csdn抽风了吗
- centos7自动化安装
- 每天一个Linux命令(42)watch命令
- Python之路【第二篇】:Python基础(一)
- 数据结构与算法之三 深入学习排序
- 数据结构与算法之三 深入学习排序
- 数据结构与算法之三 深入学习排序
- 教你安装漂亮的Arc GTK主题
- 引导工具GRUB详解