2676 3-7 类的友元函数的应用
3-7 类的友元函数的应用
#include<iostream>
#include<cmath>
using namespace std;
int n;
class point
{
private:
double x;
double y;
public :
point (){x=0;y=0;}
point (double a,double b){x=a;y=b;}
void display();
void display1();
friend void display2(point &p1,point &p2);
};
void point::display()
{
cout << "The first point is the coordinate:X=" << x << ",Y=" << y << endl;
}
void point::display1()//有无friend 的区别
{
cout << "The second point is the coordinate:X=" << x << ",Y=" << y << endl;
}
void display2(point &p1,point &p2)//有无friend 的区别
{
double c;
c=sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
cout << "The distance between the two points is:" <<c<< endl;
}
int main()
{
double x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
point p1(x1,y1);
point p2(x2,y2);
p1.display();
p2.display1();
display2(p1,p2);
}
还没有评论,来说两句吧...