友元函数

#include<iostream>
#include<math.h>
using namespace std;
class point{
	private:
		double x,y;
	public:
		point(double i,double j)
		{
			point::x=i;
			point::y=j;
		}
		void print()
		{
			cout<<x<<","<<y<<endl;
		}
		friend double dis(point a,point b);
		
};
double dis(point a,point b)
{
	double dx=a.x-b.x;
	double dy=a.y-b.y;
	return sqrt(dx*dx+dy*dy);
}

int main()
{
	point p1(3,4);
	point p2(6,8);
	p1.print();
	p2.print();
	
	cout<<dis(p1,p2)<<endl;
	return 0;
}
#include<iostream>
using namespace std;
class X{
	friend class Y;
	public:
		X(int i)
		{
			X::x=i;
		}
		void print()
		{
			cout<<x<<","<<s<<endl;
		}
	private:
		int x;
		static int s;
			
};
int X::s=5;
class Y{
	public:
		Y(int i)
		{
			y=i;
		}
		void print(X &r)
		{
			cout<<r.x<<","<<y<<endl;
		}
	private:
		int y;
};
int main()
{
	X m(2);
	m.print();
	Y n(8);
	n.print(m);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值