注册 登录
编程论坛 VC++/MFC

求高手,tell以下程序运行结果。。。

山之子 发布于 2010-11-04 12:49, 509 次点击
#include<iostream.h>
#include<math.h>
class Point
{
public:
    Point(int xx=0,int yy=0){X=xx;Y=yy;cout<<"point 构造函数被调用"<<endl;}
    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<<"X="<<X<<"Y="<<Y<<"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;
}
4 回复
#2
cnfarer2010-11-04 14:21
你可以运行一下,对照结果,应该还是不难理解的。
#3
zfan852010-11-04 16:44
自己运行是最好的方法
#4
山之子2010-11-04 19:10
回复 2楼 cnfarer
理解不了啊,能不能告诉我具体过程啊,谢谢。。。
#5
sunmingchun2010-11-04 22:57
很好理解啊
1