可否帮忙分析一下这段程序?

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

大家可以为我具体讲解一下吗?是如何执行?拷贝构造函数又为什么会调用四次呢?
谢谢!
...全文
29 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
uu2pro 2003-08-23
  • 打赏
  • 举报
回复
奇怪啊
Distance::Distance(Point xp1,Point xp2):p1(xp1),p2(xp2)
初始化列表一般是跟基类class名字的,
这里怎么跟的是成员对象的名字,而且也可以编译通过??
这是为什么?

请高手解释一下
好吗?
dxsh 2003-08-23
  • 打赏
  • 举报
回复
哦,是啊才刚学,概念还不牢固。
谢谢各位!
一起努力吧!
catface0511 2003-08-23
  • 打赏
  • 举报
回复
我是新人 看的懂 但是说不清楚
好象楼主的基本概念还是不太杂实
建议再看下书吧
这个急不来的有时候强行反而效果不好
入门的钱能那本书 我看了3遍


个人意见 祝早日进步!
summer6074 2003-08-23
  • 打赏
  • 举报
回复
楼上怎么不早几天贴啊?这个就是我们的考试题目啊!哎,错了一点点哦,最多只有80多了。。。。
fzx 2003-08-23
  • 打赏
  • 举报
回复
参数改成常量引用就好了,防止出错,效率高,又安全。
ppm07 2003-08-23
  • 打赏
  • 举报
回复
哦,我明白了。初始化成员列表原来调用的是拷贝构造函数来进行初始化的,看起来碰到这种情况还是不用它为好。
uu2pro 2003-08-23
  • 打赏
  • 举报
回复
to def1981(随机) (
继承类和包含对象是两码事
包含一般是
Distance::Distance(Point &xp1,Point &xp2) //改成引用
{
p1 = xp1; //不用初始化列表
p2 = xp2;
def1981 2003-08-23
  • 打赏
  • 举报
回复
distance 类里的成员有两个是point啊
class Distance
{
public:
Distance(Point &xp1,Point &xp2); //改成引用
double GetDis(){return dist;}
private:
Point p1,p2;
double dist;
};
所以在构造函数里要写到它们啊
楼上对类的构造函数的理解 只得其形 未得其意啊
RookieStar 2003-08-22
  • 打赏
  • 举报
回复
关键是这句 Distance(Point xp1,Point xp2):p1(xp1),p2(xp2);

在main()函数中,先产生了两个Point对象:Point myp1(1,1),myp2(4,5);
当调用 Distance myd(myp1,myp2);时,由于Distance用的是值传递,且传递的是Point对象myp1与myp2,所以在产生两个形参变量的时候,用的是Point的拷贝构造函数,这对应了输出中的第一第二行。

然后才真正意义上开始执行Distance的构造函数:初始化表中两句 p1(xp1)和p2(xp2),用的是Point的拷贝构造函数,这对应了输出中的第三第四行。

最后,执行Distance的函数体,也就对应了输出中的第五行。

楼主的基本概念还不够扎实,找本好书补一下吧!祝你早日进步!
ppm07 2003-08-22
  • 打赏
  • 举报
回复
我改了一下,
#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;
}
结果是:
Distance构造函数被调用
the Distance is:5
Press any key to continue
但我也不明白,为什么Distance::Distance(Point &xp1,Point &xp2):p1(xp1),p2(xp2)。。。这个也会有Point的拷贝构造函数被调用,清高人解释一下。
zhang,qiuping 2003-08-22
  • 打赏
  • 举报
回复
Distance::Distance(Point xp1,Point xp2):p1(xp1),p2(xp2)
//关键之处在于这,xp1,xp2只是作为值参,以上把xp1,xp2付给p1,p2 这个过程需要
再次调用Point的拷贝构造函数

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧