| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 161 人关注过本帖
标题:拷贝构造函数的问题
收藏  订阅  推荐  打印 
colorlemon
Rank: 1
等级:新手上路
帖子:4
积分:142
注册:2008-8-19
拷贝构造函数的问题

#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-8-20 15:10
xlh5225
Rank: 2
等级:注册会员
帖子:146
积分:1682
注册:2007-8-14

这是基础问题,请自己看书...没必要在这里来问
2008-8-20 15:18
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.049522 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved