| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 568 人关注过本帖
标题:拷贝构造函数的问题
只看楼主 加入收藏
colorlemon
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-8-19
收藏
 问题点数:0 回复次数:1 
拷贝构造函数的问题
#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的地址吗?
搜索更多相关主题的帖子: 函数 构造 拷贝 
2008-08-20 15:10
xlh5225
Rank: 2
等 级:论坛游民
威 望:2
帖 子:188
专家分:25
注 册:2007-8-14
收藏
得分:0 
这是基础问题,请自己看书...没必要在这里来问
2008-08-20 15:18
快速回复:拷贝构造函数的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015628 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved