主题:[讨论]构造函数的值
#include<math.h>
#include<iostream.h>
class point
{
private:
int x,y;
public:
point (int xx=0,int yy=0) {
x=xx; y=yy;
}
point (point &p);
int getx() { return x; }
int gety() { return y; }
};
point::point(point &p)
{
x=p.x; y=p.y ;
cout<<"("<<x<<","<<y<<")"<<"point 拷贝构造函数被调用"<<endl;
}
class distance
{
private :
point p1,p2;
double dist;
public :
distance(point xp1,point xp2);
double getdis() {
return dist;
}
};
distance::distance(point p1,point p2)//:p1(xp1),p2(xp2)
{
cout<<"distance 拷贝构造函数被调用"<<endl;
double x=double(p1.getx()-p2.getx());
double y=double(p1.gety()-p2.gety());
dist = sqrt(x*x+y*y);
}
void main()
{
point myp1(1,1),myp2(4,5);
point mm((point)(1,1));
cout<<endl;
distance myd((point)(1,1),(point)(6,13));
cout<<"the distance is:";
cout<<myd.getdis()<<endl;
}
为什么构造函数中传入的点参数 y的值是 0?
但是根据得到的距离 12 ,说明传入的两点 (1,1) 和(6,13)是正确的呀?可是显示却为什么出错呢?
#include<iostream.h>
class point
{
private:
int x,y;
public:
point (int xx=0,int yy=0) {
x=xx; y=yy;
}
point (point &p);
int getx() { return x; }
int gety() { return y; }
};
point::point(point &p)
{
x=p.x; y=p.y ;
cout<<"("<<x<<","<<y<<")"<<"point 拷贝构造函数被调用"<<endl;
}
class distance
{
private :
point p1,p2;
double dist;
public :
distance(point xp1,point xp2);
double getdis() {
return dist;
}
};
distance::distance(point p1,point p2)//:p1(xp1),p2(xp2)
{
cout<<"distance 拷贝构造函数被调用"<<endl;
double x=double(p1.getx()-p2.getx());
double y=double(p1.gety()-p2.gety());
dist = sqrt(x*x+y*y);
}
void main()
{
point myp1(1,1),myp2(4,5);
point mm((point)(1,1));
cout<<endl;
distance myd((point)(1,1),(point)(6,13));
cout<<"the distance is:";
cout<<myd.getdis()<<endl;
}
为什么构造函数中传入的点参数 y的值是 0?
但是根据得到的距离 12 ,说明传入的两点 (1,1) 和(6,13)是正确的呀?可是显示却为什么出错呢?