主题:[原创]类的组合
#include<iostream.h>
#include<math.h>
class Point
{
private:
float x,y;
public:
Point(float xx,float yy)
{
cout<<"point构造函数"<<endl;
}
Point(Point &p)
{
this->x=p.x;this->y=p.y;
cout<<"pont 拷贝构造函数"<<endl;
}
float GetX(void){return x;}
float GetY(void){return y;}
};
class Distance
{
private:
Point p1,p2;
double dist;
public:
Distance(Point a,Point b);//构造函数
double GetDis(void){return dist;}
};
Distance::Distance(Point a,Point b):p1(a),p2(b)
{ //有了对象成员Point a传给p1(a), Point b传给p2(b),
//double p传给p,price=p;
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);
Distance myd(myp1,myp2);
cout<<"the distance is: ";
cout<<myd.GetDis()<<endl;
}
为什么distance变成0了?
#include<math.h>
class Point
{
private:
float x,y;
public:
Point(float xx,float yy)
{
cout<<"point构造函数"<<endl;
}
Point(Point &p)
{
this->x=p.x;this->y=p.y;
cout<<"pont 拷贝构造函数"<<endl;
}
float GetX(void){return x;}
float GetY(void){return y;}
};
class Distance
{
private:
Point p1,p2;
double dist;
public:
Distance(Point a,Point b);//构造函数
double GetDis(void){return dist;}
};
Distance::Distance(Point a,Point b):p1(a),p2(b)
{ //有了对象成员Point a传给p1(a), Point b传给p2(b),
//double p传给p,price=p;
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);
Distance myd(myp1,myp2);
cout<<"the distance is: ";
cout<<myd.GetDis()<<endl;
}
为什么distance变成0了?