| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 364 人关注过本帖
标题:求教关于两个源代码中自定义运算符并且输出的问题
只看楼主 加入收藏
c453413516
Rank: 1
等 级:新手上路
帖 子:16
专家分:4
注 册:2010-12-5
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:2 
求教关于两个源代码中自定义运算符并且输出的问题
这是书上的两个关于重载操作符再输出的两个源代码:(关于mian函数里面中将类对象输出的问题,为什么上面这个源代码可以直接用"cout<<"将类对象输出,而下面这个却不可以?),是否是因为上面的源代码有一个“类型转换的成员函数”?那么这个类型转换成员函数有什么用?怎么用?
#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
class real
{
public:
        real(double value=0);
        real operator +(const real real1);                                                //加法运算
        friend real operator *(const real real1,const real &real2);                //乘运算
        friend real operator /(const real real1,const real &real2);                //除法运算
        real operator -(const real real1);                                                //减法运算
        double operator +(const double d);                                                //加法运算
        real operator +(const int i);                                                        //加法运算
        
        int operator !()const;                                //非运算
        real operator -()const;                        //求负
        real operator +()const;                        //求正
        real operator ++();                                //右自增
        real operator --();                                //右自减
        operator double()const;                        //类型转换
private:
        double value;
};
//加法运算
real real:perator +(const int i)
{
        real temp;
        temp.value=value+i;
        return temp;
}
//加法运算
double real:perator +(const double d)
{
        return value+d;
}
//非运算
int real:perator !()const
{
        return !value;
}
//减法运算
real real:perator -(const real real1)
{
        return real(this->value-real1.value);
}
//除法运算
real operator /(const real real1,const real &real2)
{
        real temp;
        assert(real2.value);
        temp.value=real1.value/real2.value;
        return temp;
}
//构造函数
real::real(double value)
{
        this->value=value;
}
//加法运算
real real:perator +(const real real1)
{
        return real(this->value+real1.value);
}
//求负
real real::operator -()const
{
        return real(-value);
}
//求正
real real::operator +()const
{
        return real(+value);
}
//右自增
real real::operator ++()
{
        value++;
        return real(value);
}
//右自减
real real::operator --()
{
        value--;
        return real(value);
}
//类型转换
real::operator double()const
{
        return value;
}
//乘法运算
real operator *(const real real1,const real &real2)
{
        real temp;
        temp.value=real1.value*real2.value;
        return temp;
}
int main(int argc, char* argv[])
{
        real r1(12.3);
        real r2(20.8);
        cout<<r1+r2<<endl;
        cout<<-r1<<endl;
        cout<<r1*r2<<endl;
        cout<<++r1<<endl;
        cout<<r1<<endl;
        cout<<--r1<<endl;
        cout<<r1<<endl;
        return 0;
}

下面的:
#include "stdafx.h"
#include <iostream>
using namespace std;
class real                                        //实数类
{
public:
        real(double value=0);
        real operator -()const;        //重载"-"负运算
        real operator ++();                //重载"++"自增运算
private:
        double value;
};
real::real(double value)
{
        this->value=value;
}
real real::operator -()const
{
        return real(-value);
}
real real::operator ++()
{
        value++;
        return real(value);
}
int main(int argc, char* argv[])
{
        real r1(12.3);
        cout<<r1<<endl;
        cout<<++r1<<endl;
        cout<<r1<<endl;
        cout<<-r1<<endl;
        return 0;
}

多谢了!!谢谢!!!
搜索更多相关主题的帖子: 源代码 
2011-05-30 12:32
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
你说得对
2011-05-30 13:15
c453413516
Rank: 1
等 级:新手上路
帖 子:16
专家分:4
注 册:2010-12-5
收藏
得分:0 
看来真的是这样……
2011-06-01 12:32
快速回复:求教关于两个源代码中自定义运算符并且输出的问题
数据加载中...
 
   



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

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