源程序:
#include <iostream>
using namespace std;
class Point
{
private:
int X,Y;
public:
Point(int a=0,int b=0)
{
X=a;
Y=b;
cout<<"Initializing……"<<endl;
}
Point(const Point &p); //复制构造函数
int Getx()
{
return X;
}
int Gety()
{
return Y;
}
void show()
{
cout<<"X="<<X<<",Y="<<Y<<endl;
}
~Point()
{
cout<<"delete……"<<X<<","<<Y<<endl;
}
};
Point::Point(const Point &p)
{
X=p.X;
Y=p.Y;
cout<<"Copy Initializin……"<<endl;
}
void display(Point p)
{
p.show();
}
void disp(Point &p)
{
p.show();
}
Point fun()
{
Point A(101,202);
return A;
}
int main()
{
Point A(42,35);
Point B(A); //B=A
Point C(58,94);
cout<<"called display(B)"<<endl;
display(B);
cout<<"Next..."<<endl;
cout<<"called disp(B)"<<endl;
disp(B);
cout<<"call C=fun()"<<endl;
C=fun();
cout<<"called disp(C)"<<endl;
disp(C);
cout<<"out..."<<endl;
return 1;
}
运行结果: