注册 登录
编程论坛 C++教室

拷贝构造函数的问题

colorlemon 发布于 2008-08-20 15:10, 572 次点击
#include "iostream.h"
class  CComplex
{
private:
    double real;
    double imag;
public:
    CComplex();
    CComplex(double r, double i);
    CComplex(CComplex &c);       //复数类的拷贝构造函数声明
    void Set(double r, double i);
    void Print();
    CComplex Add(CComplex c);
    CComplex Sub(CComplex c);
};
CComplex::CComplex()
{
    real = 0.0;
    imag = 0.0;
}
CComplex::CComplex (double  r, double i)
{
    real = r;
    imag = i;
}
CComplex::CComplex (CComplex &c)     //复数类的拷贝构造函数定义
{
    real = c.real;
    imag = c.imag;
}
void CComplex::Set(double r, double i)
{
    real = r;
    imag = i;
}
void CComplex::Print()
{
    cout << "(" << real << "," << imag << ")" << endl;
}
CComplex CComplex::Add(CComplex c)
{
    CComplex temp;
    temp.real = real + c.real;
    temp.imag = imag + c.imag;
    return temp;
}
CComplex CComplex::Sub(CComplex c)
{
    CComplex temp;
    temp.real = real - c.real;
    temp.imag = imag - c.imag;
    return temp;
}
void main(void)
{
    CComplex  a, b(3.0,4.0), c;
    CComplex  d(b);       //调用复数类的拷贝构造函数
    cout << "a = ";
    a.Print();
    cout << "b = ";
    b.Print();
    cout << "d = ";
    d.Print();
    c = b.Add(d);
    d = a.Sub(d);
    cout << "c = ";
    c.Print();
    cout << "d = ";
    d.Print();
}

这是一个复述类的程序(书上的)为什么CComplex  d(b)时调用复数类的拷贝构造函数?

CComplex::CComplex (CComplex &c)这句定义也不懂.&c不应该是c的地址吗?
1 回复
#2
xlh52252008-08-20 15:18
这是基础问题,请自己看书...没必要在这里来问
1