日志18
面向对象 求点间距离:
#include<iostream>
#include<cmath>
using namespace std;
class point
{
protected:
int X,Y;
public:
void set(int x,int y)
{
X=x;Y=y;
}
int getX()
{
return X;
}
int getY()
{
return Y;
}
point() //初始化 也可写 point():X(0),Y(0){} 只允许在 构造函数 时 用初始化列表
{
X=Y=0;
}
point(int x,int y) //初始化列表 point(int x,int y):X(x),Y(y){}
{
X=x;Y=y;
}
/* 21-28行合并:(无参与带参合并)
point(int x=0,int y=0):X(x),Y(y){}
*/
double distO()
{
return sqrt(X*X+Y*Y);
}
double dist(point &p)
{
int x,y;
x=X-p.X;
y=Y-p.Y;
return sqrt(x*x+y*y);
}
void show()
{
cout<<"point("<<this->X<<","<<this->Y<<")\n";
}
};
int main()
{
point p1,p2(3,4);
p1.show();
p2.show();
p2.set(3,5);
cout<<"p1-o"<<"="<<p1.distO()<<endl;;
cout<<"p1-p2"<<"="<<p1.dist(p2)<<endl;;
}
#include<iostream>
#include<cmath>
using namespace std;
class point
{
protected:
int X,Y;
public:
void set(int x,int y)
{
X=x;Y=y;
}
int getX()
{
return X;
}
int getY()
{
return Y;
}
point() //初始化 也可写 point():X(0),Y(0){} 只允许在 构造函数 时 用初始化列表
{
X=Y=0;
}
point(int x,int y) //初始化列表 point(int x,int y):X(x),Y(y){}
{
X=x;Y=y;
}
/* 21-28行合并:(无参与带参合并)
point(int x=0,int y=0):X(x),Y(y){}
*/
double distO()
{
return sqrt(X*X+Y*Y);
}
double dist(point &p)
{
int x,y;
x=X-p.X;
y=Y-p.Y;
return sqrt(x*x+y*y);
}
void show()
{
cout<<"point("<<this->X<<","<<this->Y<<")\n";
}
};
int main()
{
point p1,p2(3,4);
p1.show();
p2.show();
p2.set(3,5);
cout<<"p1-o"<<"="<<p1.distO()<<endl;;
cout<<"p1-p2"<<"="<<p1.dist(p2)<<endl;;
}
全部评论
相关推荐
06-12 17:46
门头沟学院 Java 
点赞 评论 收藏
分享
06-06 17:27
天津工业大学 golang 点赞 评论 收藏
分享