开发者社区> 问答> 正文

C++ 类 如何重载运算符

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

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

    2020-08-03 20:12:55
    赞同 展开评论 打赏
  • 软件开发,安全加密

    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
    赞同 展开评论 打赏
  • 乐于学习与分析

    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
    赞同 展开评论 打赏
  • #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
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载