| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 582 人关注过本帖
标题:关于操作符的重载,老是出错。。
只看楼主 加入收藏
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
结帖率:91.43%
收藏
已结贴  问题点数:20 回复次数:3 
关于操作符的重载,老是出错。。
真的不习惯C++语法。。

程序代码:
#include <iostream>
using namespace std;
class money
{
    public:
        money(int _dollars, int _cents);
        void showmoney();
        int getdollars()
            return dollars;
        int getcents()
            return cents;
    private:
        int dollars;
        int cents;
};
money::money(int _dollars, int _cents){
    dollars = _dollars;
    cents =_cents;
}
void money::showmoney(){
    cout << "Money:" << dollars << " . " << cents << endl;
}
const money operator +(const money &a, const money &b){
    int _dollars = a.getdollars() + b.getdollars();
    int _cents = a.getcents() + b.getcents();
    return money(_dollars, _cents);
}
int main(){
    money my(100, 25); 
    money your(200, 50);
    money our = my + your;
    our.showmoney();
    return 0;
}
搜索更多相关主题的帖子: dollars money color 
2011-10-08 22:42
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
谢谢啦。各位。。。

o(∩∩)Linux & Python 群:187367181
2011-10-08 22:42
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
调一下排版。。。

程序代码:
#include <iostream>
using namespace std;
class money
{
    public:
        money(int _dollars, int _cents);
        void showmoney();
        int getdollars()
        {
            return dollars;
        }
        int getcents()
        {
            return cents;
        }
    private:
        int dollars;
        int cents;
};
money::money(int _dollars, int _cents)
{
    dollars = _dollars;
    cents =_cents;
}
void money::showmoney()
{
    cout << "Money:" << dollars << " . " << cents << endl;
}
money operator +(const money &a, const money &b)
{
    int _dollars = a.getdollars() + b.getdollars();
    int _cents = a.getcents() + b.getcents();
    return money(_dollars, _cents);
}
int main()
{
    money my(100, 25); 
    money your(200, 50);
    money our = my + your;
    our.showmoney();
    return 0;
}

o(∩∩)Linux & Python 群:187367181
2011-10-08 22:47
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:20 
程序代码:
class money
{
    public:
        money(int _dollars, int _cents);
        void showmoney();
        int getdollars()const //
        {
            return dollars;
        }
        int getcents()const //
        {
            return cents;
        }
    private:
        int dollars;
        int cents;
};

迭代的是人,递归的是神。
2011-10-08 23:32
快速回复:关于操作符的重载,老是出错。。
数据加载中...
 
   



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

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