private:
double x
double y
public:
Point()
Point(double x1, double y1) {x=x1y=y1}
void set(double x1, double y1) {x=x1y=y1}
void Display() {cout <<"(x,y)=(" <<x <<"," <<y <<")" <<endl
void move(double x1, double y1) {x=x1y=y1}
double getx() {return x}
double gety() { return y}
double Distance(Point &p) {
return sqrt( pow((p.getx()-x), 2) + pow((p.gety()-y),2))
}
}
该类可以提供移动,求到另一点的距离,获取X坐标和Y坐标等 *** 作,也可以设置X坐标和Y坐标的值。要求有拷贝构造函数。数据成员为X坐标和Y坐标。
源代码如下
using namespace std
class Point{
public:
Point()//不带参数构造函数
Point(int X, int Y)//带参数的构造函数
Point(const Point &p)//拷贝构造函数;
int getX()//获取横坐标
int getY()//获取纵坐标
void setX(int iX)//设置横坐标
void setY(int iY)//设置纵坐标
float distance(Point p1, Point p2)//计算两点的距离protected:
private:
int x//横坐标
int y//纵坐标
}
int main()
{
Point pos
Point pt(10, 10)
Point pts(pt)
cout <<"获取pos的坐标值" <<endl
cout <<"X:" <<pos.getX() <<endl
cout <<"Y:" <<pos.getY() <<endl
pos.setX(20)
pos.setY(20)
cout <<"获取pos的设置后的坐标值" <<endl
cout <<"X:" <<pos.getX() <<endl
cout <<"Y:" <<pos.getY() <<endl
cout <<"获取pt的坐标值" <<endl
cout <<"X:" <<pt.getX() <<endl
cout <<"Y:" <<pt.getY() <<endl
cout <<"获取pts的坐标值" <<endl
cout <<"X:" <<pos.getX() <<endl
cout <<"Y:" <<pos.getY() <<endl
cout <<"输出pos与pt之间的距离" <<endl
cout <<"X:" <<pos.distance(pos, pt) <<endl
return 0
}
扩展资料
一个点类Point,有2个私有数据成员x,y代表点坐标的源代码
include
#include
using namespace std
class Point
{
public:
Point(int a,int b):x(a),y(b){cout<<"初始化点类的一个对象\n"}
~Point(){cout<<"回收点类的内存空间\n"}
void show()
{cout<<"这个点的坐标为:"<<"("<double distance(Point &p){return sqrt((p.y-y)
(p.x-y)+(p.x-x)*(p.x-x))}
private:
int x
int y
}
int main()
{
Point a(0,0)
Point b(1,1)
cout<return 0
}
class Point{
int a
int b
/// <summary>
/// 有参
/// </summary>
public Point(int c)
{
this.a = c
}
/// <summary>
/// 无参
/// </summary>
public Point()
{
this.a = 1
this.b = 2
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)