c++第三次实验——作业
2016-04-09 19:22
411 查看
项目2:友元函数
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
private:
double x; // 横坐标
double y; // 纵坐标
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy) {}
//请继续写需要的代码
double distance1(CPoint &);
friend double distance2(CPoint &,CPoint &);
double getx()
{
return x;
}
double gety()
{
return y;
}
};
double CPoint::distance1(CPoint &m)
{
double x1,y1,d;
x1=m.x-x;
y1=m.y-y;
d=sqrt(x1*x1+y1*y1);
return d;
}
double distance2(CPoint &m1,CPoint &m2)
{
double x1,y1,d;
x1=(m1.x-m2.x);
y1=(m1.y-m2.y);
d=sqrt(x1*x1+y1*y1);
return d;
}
double distance3(CPoint&m1,CPoint&m2)
{
double x1,y1,d;
x1=m1.getx()-m2.getx();
y1=m1.gety()-m2.gety();
d=sqrt(x1*x1+y1*y1);
return d;
}
int main()
{
CPoint m1(3,4),m2(1,2);
cout<<m2.distance1(m1)<<endl; //成员函数
cout<<distance2(m1,m2)<<endl; //友员函数
cout<<distance3(m1,m2)<<endl; //一般函数
return 0;
}
运行结果:
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
private:
double x; // 横坐标
double y; // 纵坐标
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy) {}
//请继续写需要的代码
double distance1(CPoint &);
friend double distance2(CPoint &,CPoint &);
double getx()
{
return x;
}
double gety()
{
return y;
}
};
double CPoint::distance1(CPoint &m)
{
double x1,y1,d;
x1=m.x-x;
y1=m.y-y;
d=sqrt(x1*x1+y1*y1);
return d;
}
double distance2(CPoint &m1,CPoint &m2)
{
double x1,y1,d;
x1=(m1.x-m2.x);
y1=(m1.y-m2.y);
d=sqrt(x1*x1+y1*y1);
return d;
}
double distance3(CPoint&m1,CPoint&m2)
{
double x1,y1,d;
x1=m1.getx()-m2.getx();
y1=m1.gety()-m2.gety();
d=sqrt(x1*x1+y1*y1);
return d;
}
int main()
{
CPoint m1(3,4),m2(1,2);
cout<<m2.distance1(m1)<<endl; //成员函数
cout<<distance2(m1,m2)<<endl; //友员函数
cout<<distance3(m1,m2)<<endl; //一般函数
return 0;
}
运行结果:
相关文章推荐
- C++——多次free和delete导致错误
- Let's Complain the Irresponsible keyword 'const' in C++
- c++作业3
- C++泛型之求未知数组的长度
- C++实验三
- c++模板实现栈
- 2016年蓝桥杯C++/c B组答案
- Getting Started with C++ in Visual Studio
- C++类内存分布
- C语言超大数相加求和、加减乘除算法实现
- C语言常用转义字符表 和 ASCII码表完整版
- 递归和尾递归的比较,斐波那契
- 3.10 左值和右值 中英文对照(C++标准中文版 ISO/IEC 14882:2014)
- C++虚继承内存布局
- C语言编程“Hello,world”
- Google的c++代码规范
- 队列的C++实现
- C++中的exit()函数
- 希尔排序算法的C语言实现示例
- C++ 读写文件