c++ 能用得

来源:百度知道 编辑:UC知道 时间:2024/06/08 12:04:51
设计Dot类,包括私有数据x,y表示一个点坐标,含有构造函数,拷贝构造函数实现对点放大10倍,成员函数move()可以对点移动形参指定的距离,成员函数show()用于显示点坐标。设计友元函数Distance(),用于求两点之间的距离。主函数定义2个点对象,输出两个点之间的距离

#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

class Dot
{
private:
int x;
int y;

public:
Dot( ): x(0),y(0)
{ }

Dot(int a,int b)
{
x=a*10;
y=b*10;
}

void move(int m,int n)
{
x += m;
y += n;
}

void show()
{
cout<<"\nX is "<<x;
cout<<"\nY is "<<y;
}

friend float dis(Dot,Dot);
};

float dis(Dot d1,Dot d2)
{
float d=sqrt(pow(d2.x-d1.x,2)+pow(d2.y-d1.y,2) );
return d;
}

int main()
{
Dot d1(3,4);
Dot d2(2,3);
float s=dis(d1,d2);
cout<<"\nThe distance is: "<<s<<endl;
return 0;
}

不知道你说的“成员函数move()可以对点