分类: C/C++
2011-08-03 21:33:50
/*
* 复制构造函数
* 用一个已知的对象初始化另一个对象
* Lzy 2011-8-3
*/
#include
using namespace std;
class Complex
{
private:
double rel, img;
public:
Complex(double x, double y):rel(x),img(y){}
Complex(const Complex &c):rel(c.rel),img(c.img){cout<<"构造函数被调用"<
void GerRel(){cout<
void GerImg(){cout<
};
int main(void)
{
Complex c1(2.3,5.6);
c1.GerImg();
c1.GerRel();
Complex c2(c1);
c2.GerImg();
c2.GerRel();
return 0;
}
/*
* 复制初始化构造函数的其他用法
* Lzy 2011-8-3
*/
#include
using namespace std;
class Complex
{
private:
double rel, img;
public:
Complex(double x=5.0, double y=3.0):rel(x),img(y){}
Complex(const Complex &c):rel(c.rel),img(c.img){cout<<"复制构造函数被调用"<
double GetRel(void){cout<
double GetImg(void){cout<return img;}
};
Complex fun(Complex c)
{
double x, y;
x = c.GetRel()*10;
y = c.GetImg()*10;
Complex temp(x,y);
return temp;
}
int main(void)
{
Complex c1(1.1, 2.2),c2;
c2 = fun(c1);
c2.GetRel();
c2.GetImg();
return 0;
}