構造函數和拷貝構造函數的實例
#include<iostream.h>
class Point
{ public: //共有數據 外部接口
Point(int xx=0,int yy=0) //構造函數
{
X=xx;
Y=yy;
}
Point(Point &P); // 拷貝構造函數
int GetX()
{ return X;}
int GetY()
{ retrun Y;}
private:
int X, int Y; // 私用數據
};
// 成員函數的實現
Point ::Point(Point &P)
{ X=P.X;
Y=P.Y;
cout<<"拷貝構造函數被調用"<<endl;
}
void fun1(Point P)
{ cout<<P.GetX()<<endl;
}
void fun2()
{ Point A(1,2)
retrun A;
}
void main()
{ Point A(4,5);
Point B(A);
cout<<B.GetX()<<endl;
fun1(B);
B=fun2();
cout<<B.GetX()<<endl;
}