标题:关于操作符的重载,老是出错。。
只看楼主
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
帖 子:605
专家分:2489
注 册: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-351835-1-1.html




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

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