主题:类的组合,求助!!!
[size=3]#include <iostream>
#include <cmath>
using namespace std;
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<<"Poin拷贝构造函数被调用"<<endl;
}
class Line
{
public:
Line(Point xp1,Point xp2);
Line(Line &L);
double GetLen() {return len;}
private:
Point p1,p2;
double len;
};
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2)
{
cout<<"Line构造函数被调用"<<endl;
double x=double(p1.GetX()-p2.GetX());
double y=double(p1.GetY()-p2.GetY());
len=sqrt(x*x+y*y);
}
Line::Line(Line &L):p1(L.p1),p2(L.p2)
{
cout<<"Line拷贝构造函数被调用"<<endl;
len=L.len;
}
void main()
{
Point myp1(1,1),myp2(4,5);
Line A(myp1,myp2);
Line B(A);
cout<<"The length of the line is:";
cout<<A.GetLen()<<endl;
cout<<"The length of the line2 is:";
cout<<B.GetLen()<<endl;
}[/size]
[color=008000][size=3]运行结果是:
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Line构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Line拷贝构造函数被调用
The length of the line is:5
The length of the line2 is:5[/size][/color]
[color=800000][size=2]学到“类的组合”这里,看着程序的运行结果,实在搞不懂这个程序中程序的调用函数的顺序,请大家帮我详细讲一下好吗?谢谢了!!![/size][/color]
#include <cmath>
using namespace std;
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<<"Poin拷贝构造函数被调用"<<endl;
}
class Line
{
public:
Line(Point xp1,Point xp2);
Line(Line &L);
double GetLen() {return len;}
private:
Point p1,p2;
double len;
};
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2)
{
cout<<"Line构造函数被调用"<<endl;
double x=double(p1.GetX()-p2.GetX());
double y=double(p1.GetY()-p2.GetY());
len=sqrt(x*x+y*y);
}
Line::Line(Line &L):p1(L.p1),p2(L.p2)
{
cout<<"Line拷贝构造函数被调用"<<endl;
len=L.len;
}
void main()
{
Point myp1(1,1),myp2(4,5);
Line A(myp1,myp2);
Line B(A);
cout<<"The length of the line is:";
cout<<A.GetLen()<<endl;
cout<<"The length of the line2 is:";
cout<<B.GetLen()<<endl;
}[/size]
[color=008000][size=3]运行结果是:
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Line构造函数被调用
Point拷贝构造函数被调用
Point拷贝构造函数被调用
Line拷贝构造函数被调用
The length of the line is:5
The length of the line2 is:5[/size][/color]
[color=800000][size=2]学到“类的组合”这里,看着程序的运行结果,实在搞不懂这个程序中程序的调用函数的顺序,请大家帮我详细讲一下好吗?谢谢了!!![/size][/color]