| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 570 人关注过本帖
标题:拷贝构造函数的问题
取消只看楼主 加入收藏
colorlemon
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-8-19
收藏
 问题点数:0 回复次数:0 
拷贝构造函数的问题
#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
快速回复:拷贝构造函数的问题
数据加载中...
 
   



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

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