| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1285 人关注过本帖
标题:我定义了个复数类的运算符重载,为什么不能访问私有数据
只看楼主 加入收藏
winnerflyer
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2008-3-14
收藏
 问题点数:0 回复次数:2 
我定义了个复数类的运算符重载,为什么不能访问私有数据
我定义了个复数类的运算符重载,为什么不能访问私有数据,但是书上这样可以啊,
另外我定义的一个分数类运算符重载,它可以访问私有数据.........

复数类:

#include <iostream.h>
#include <stdio.h>
#include <math.h>

class Complex
{
private:
    double real;
    double imag;
public:
    
    //double real;
    //double imag;

    Complex()
    {
    }
    Complex(double x,double y)
    {
            real=x;
            imag=y;
    }
    
    Complex(char *d)
    {
        char look;
        sscanf(d,"%lf%c",&real,&look);
        if(look=='-'||look=='+')
        {
            
            sscanf(d,"%lf%c%lf%*c",&real,&look,&imag);
            if(look=='-')
                imag=-imag;
        }
        else imag=0;
        
    }
    friend ostream& operator<<(ostream&,const Complex &temp);
    friend istream& operator>>(ostream&,const Complex &temp);
    Complex operator+(const Complex &com);
    Complex operator-(const Complex &com);
    Complex operator*(const Complex &com);
    Complex operator/(const Complex &com);
    Complex operator+(double d);
    Complex operator-(double d);
    Complex operator*(double d);    
    Complex operator/(double d);
    friend Complex operator+(double d,const Complex &com);
    friend Complex operator-(double d,const Complex &com);    
    friend Complex operator*(double d,const Complex &com);    
    friend Complex operator/(double d,const Complex &com);
    int operator==(const Complex &);
    int operator!=(const Complex &);
    
    void Display() const
    {
        cout<<endl<<real;
        if(imag>0)
        {
            cout<<"+";
            cout<<imag<<"i"<<endl;
        }
        else if(imag==0);
        else cout<<imag<<"i"<<endl;

    }

};


    ostream& operator<<(ostream& out,const Complex &temp)
    {
        temp.Display();
        return out;
    }

    istream& operator>>(istream& in, Complex &temp)
    {
        char d[10];
        char look;
        cin>>d;
        sscanf(d,"%lf%c",&temp.real,&look);
        if(look=='-'||look=='+')
        {
            
            sscanf(d,"%lf%c%lf%*c",&temp.real,&look,&temp.imag);
            if(look=='-')
                temp.imag=-temp.imag;
        }
        else temp.imag=0;
        return in;
    }

    Complex Complex::operator+(const Complex &com)
    {
        Complex temp;
        temp.real=real+com.real;
        temp.imag=imag+com.imag;
        return temp;
    }

        

    Complex operator+(double d,const Complex &com)
    {
        Complex temp;
        temp.real=com.real+d;
        temp.imag=com.imag;
        return temp;
    }

    Complex Complex::operator+(double d)
    {
        Complex temp;
        temp.real=real+d;
        temp.imag=imag;
        return temp;
    
    }

    Complex Complex::operator-(const Complex &com)
    {
        Complex temp;
        temp.real=real-com.real;
        temp.imag=imag-com.imag;
        return temp;
    }

    Complex operator-(double d,const Complex &com)
    {
        Complex temp;
        temp.real=d-com.real;
        temp.imag=com.imag;
        return temp;
    }

     Complex Complex::operator-(double d)
    {
        Complex temp;
        temp.real=real-d;
        temp.imag=imag;
        return temp;
    }

     Complex Complex::operator*(const Complex &com)
    {
        Complex temp;
        temp.real=(com.real)*real+(com.imag)*imag;
        temp.imag=(com.imag)*real+(com.real)*imag;
        return temp;
    }

    Complex operator*(double d,const Complex &com)
    {
        Complex temp;
        temp.real=d*com.real;
        temp.imag=com.imag*d;
        return temp;
    }
    Complex Complex::operator*(double d)
    {
        Complex temp;
        temp.real=real*d;
        temp.imag=imag*d;
        return temp;
        
    }

    Complex Complex::operator/(const Complex &com1)//////////////////////
    {
        if( (com1.imag==0)&&(com1.real==0) )
        {
            cout<<"dived con't be zero!"<<endl;
            return *this;
        }
        Complex temp,com;
        double tempp;
        com=com1;
        temp=*this;
        tempp=com.real*com.real+com.imag*com.imag;
        com.imag=-com.imag;
        temp=temp*com;
        temp=temp/tempp;
        return temp;
    }

    Complex Complex::operator/(double d)
    {
        if(d==0)
        {
            cout<<"dived con't be zero!"<<endl;
            return *this;
        }
        Complex temp;
        temp.real=real/d;
        temp.imag=imag/d;
        return temp;
        
    }

    Complex operator/(double d,const Complex &com)/////////////////////////////
    {
        if( (com.imag==0)&&(com.real==0) )
        {
            cout<<"dived con't be zero!"<<endl;
            return 0;
        }
        Complex temp,com1;
        double tempp;
        com1=com;
        com1.imag=-com1.imag;
        tempp=com.real*com.real+com.imag*com.imag;
        d=d/tempp;
        temp=d*com1;
        return temp;
    }

    int Complex::operator==(const Complex &temp)
    {
        if(real!=temp.real)
            return 0;
        if(imag!=temp.imag)
            return 0;
        return 1;
    }

    int Complex::operator!=(const Complex &temp)
    {
        if(real!=temp.real)
            return 1;
        if(imag!=temp.imag)
            return 1;
        return 0;
    }

ComplexOverload.rar (277.68 KB) 复数类



Franction.rar (274.87 KB) 分数类

搜索更多相关主题的帖子: 运算符 定义 数据 重载 访问 
2008-06-02 18:58
beyond0702
Rank: 1
来 自: 桂 林
等 级:新手上路
帖 子:219
专家分:0
注 册:2007-11-17
收藏
得分:0 
我发现有好多书也都是这样写:对象直接访问私有成员。(错误)
对象 不能 访问 私有成员 的,必须通过成员函数来调用,

蝴 蝶 颤 动 了 翅 膀 !!!
2008-06-10 21:30
夜风依旧
Rank: 2
等 级:论坛游民
帖 子:42
专家分:20
注 册:2008-5-13
收藏
得分:0 
在类外实现友员函数有的编译器不支持

[b]男人,就要对自己很一点。[/b]
2008-06-10 21:39
快速回复:我定义了个复数类的运算符重载,为什么不能访问私有数据
数据加载中...
 
   



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

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