c++作业3

项目2:友元函数

#include<iostream>
using namespace std;
#include<math.h>
class CPoint      //为了方便,这里就求到原点的距离
{  
 private:    
    double x;  // 横坐标    
    double y;  // 纵坐标    
 public:   
    CPoint(double xx=0,double yy=0):x(xx),y(yy){}
	double distance1();
	friend double distance2(CPoint &);
	double get_x();
	double get_y();
}; 
double CPoint::distance1()
{
	double distance;
	distance=sqrt(x*x+y*y);
	return distance;
}
double distance2(CPoint &c)
{
	double distance;
	distance=sqrt(c.x*c.x+c.y*c.y);
	return distance;
}
double CPoint::get_x()
{
	return x;
}
double CPoint::get_y()
{
	return y;
}
double distance3(CPoint &c)
{
	double distance,a,b;
	a=c.get_x();
	b=c.get_y();
	distance=sqrt(a*a+b*b);
	return distance;
}
int main()
{
	CPoint c1(3.0,4.0);
	cout<<"distance is :"<<c1.distance1()<<endl;
	cout<<"distance is :"<<distance2(c1)<<endl;
	cout<<"distance is :"<<distance3(c1)<<endl;
	return 0;
}


你可能感兴趣的:(C++)