开发者社区 问答 正文

C++ 类 如何重载运算符

编写一个Customer类,包括账号、密码、姓名、余额(初始为0)。
用三种方法重载运算符“-”,使得两个Customer对象相减,能得到它们余额之差。
请发送至邮箱liangxiaoqi_c@163.com

展开
收起
a123456678 2016-03-06 10:46:24 3025 分享
分享
版权
举报
4 条回答
写回答
取消 提交回答
  • https://www.runoob.com/cplusplus/cpp-overloading.html 我怀疑你把大一的作业发上来了

    2020-08-03 20:12:55 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
  • 软件开发,安全加密

    using namespace std;

    class Customer
    {
    public:

    Customer(int bal):balance(bal){};
    char account[10];
    char password[10];
    char name[10];
    int balance;
    };

    int operator-(Customer& p, Customer& q)
    {

    return p.balance - q.balance;
    }

    int main(int argc, char* argv[])
    {

    Customer c1(60);
    Customer c2(50);
    cout << c1-c2 << endl;
    }

    2019-07-17 18:54:14 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
  • 乐于学习与分析

    using namespace std;

    class Customer
    {
    public:

    Customer(int bal):balance(bal){};
    char account[10];
    char password[10];
    char name[10];
    int balance;
    };

    int operator-(Customer& p, Customer& q)
    {

    return p.balance - q.balance;
    }

    int main(int argc, char* argv[])
    {

    Customer c1(60);
    Customer c2(50);
    cout << c1-c2 << endl;
    }

    2019-07-17 18:54:14 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
  • #include <iostream>
    
    using namespace std;
    
    class Customer
    {
    public:
        char account[10];
        char password[10];
        char name[10];
        int balance;
        Customer(int n)
        {
            balance = n;
        }
    };
    
    Customer operator-(Customer p, int n)
    {
        return Customer(p.balance - n);
    }
    
    int main(int argc, char* argv[])
    {
        Customer c1(100);
        Customer c2 = c1 - 10;
        cout << c2.balance << endl;
    }
    2019-07-17 18:54:14 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等