64,649
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<math.h>
using namespace std;
class point
{
private:
double x,y;
public:
point(double xx,double yy)
{
cout<<"使用点构造函数"<<endl;
x=xx;y=yy;
}
point(point &t)
{
cout<<"使用点拷贝构造函数"<<endl;
x=t.x;y=t.y;
}
double getx(){return x;}
double gety(){return y;}
};
class distances
{
private:
point p1,p2;
double disc;
public:
distances(point t1,point t2);
double getdisc(){return disc;}
};
distances::distances(point t1,point t2):p1(t1),p2(t2)
{
cout<<"使用距离构造函数"<<endl;
double x=double(t1.getx()-t2.getx());
double y=double(t1.gety()-t2.gety());
disc=sqrt(x*x+y*y);
}
int main(void)
{
double a,b,c,d;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>a>>b;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>c>>d;
point myp1(a,b),myp2(c,d);
distances myd(myp1,myp2);
cout<<"the distance is:"<<endl;
cout<<myd.getdisc()<<endl;
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
class point
{
private:
double x,y;
public:
point(double xx,double yy)
{
cout<<"使用点构造函数"<<endl;
x=xx;y=yy;
}
point(point &t)
{
cout<<"使用点拷贝构造函数"<<endl;
x=t.x;y=t.y;
}
double getx(){return x;}
double gety(){return y;}
};
class distance
{
private:
point p1,p2;
double disc;
public:
distance(point t1,point t2);
double getdisc(){return disc;}
};
distance::distance(point t1,point t2):p1(t1),p2(t2)
{
cout<<"使用距离构造函数"<<endl;
double x=double(t1.getx()-t2.getx());
double y=double(t1.gety()-t2.gety());
disc=sqrt(x*x+y*y);
}
int main(void)
{
double a,b,c,d;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>a>>b;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>c>>d;
point myp1(a,b),myp2(c,d);
distance myd(myp1,myp2);
cout<<"the distance is:"<<endl;
cout<<myd.getdisc()<<endl;
return 0;
}