可否帮忙分析一下这段程序?
dxsh 2003-08-22 07:10:15 各位我是c++菜鸟,虽然对概念有了解,但对一些具体的程序内部的执行还是不太理解,就像:
#include <iostream.h>
#include <math.h>
class Point
{public:
Point(int xx=0,int yy=0) {X=xx;Y=yy;}
Point(Point &p);
int GetX(){return X;}
int GetY(){return Y;}
private:
int X,Y;
};
Point::Point(Point &p)
{X=p.X;
Y=p.Y;
cout<<"Point拷贝构造函数被调用"<<endl;}
class Distance
{public:
Distance(Point xp1,Point xp2);
double GetDis(){return dist;}
private:
Point p1,p2;
double dist;};
Distance::Distance(Point xp1,Point xp2):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);
Distance myd(myp1,myp2);
cout<<"the Distance is:";
cout<<myd.GetDis()<<endl;
}
运行结果:
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Distance构造函数被调用
the distance is:5
大家可以为我具体讲解一下吗?是如何执行?拷贝构造函数又为什么会调用四次呢?
谢谢!