c++ 定义一个Point类,其数据成员包括一个点的坐标x、y,成员函数包括设置和读取这两个变量

2024-11-30 05:36:44
推荐回答(4个)
回答1:

#include
#include
using namespace std;
class Point
{
double x,y;
public:
Point(double a,double b)
{
x=a;y=b;
}
friend double dis(Point & a,Point & b);
};
double dis(Point & a,Point & b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void main()
{
Point A(3,4), B(3,6);
cout<}

回答2:

class Point
{
public:
Point(int _x=0,int _y=0):x(_x),y(_y) {};
Point(Point &p):x(p.x),y(p.y){};
~Point(){};

int GetX()
{
return x;
}
int GetY()
{
return y;
}
void SetX(int _x)
{
x = _x;
}
void SetY(int _y)
{
y = _y;
}
private:
int x;
int y;
}

回答3:

class point(int m,int n)
{
public:
int x,y;
setxy(m,n){x=m,y=n}
}

回答4:

class Point
{
private:
int x;
int y;
public:
void SetX(int _x){x = _x}
void SetY(int _y){y = _y}
};