csu_17
2015-05-01 01:03
采纳率: 44.4%
浏览 1.7k

C++ 编程 运算符重载

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • threenewbee 2015-05-01 01:06
    已采纳

    参考:http://blog.csdn.net/wangfengwf/article/details/11580653
    如果要完整代码,请先采纳本回答。

    已采纳该答案
    打赏 评论
  • threenewbee 2015-05-01 04:30
     // MyApp1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #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)
        {
            return Customer(balance - p.balance);
        }
    };
    
    int main(int argc, char* argv[])
    {
        Customer c1(100);
        Customer c2(10);
        Customer c3 = c1 - c2;
        cout << c3.balance << endl;
    }
    
    
    打赏 评论
  • threenewbee 2015-05-01 04:31

    另一个方式:

     // MyApp1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #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 p1, Customer p2)
    {
        return Customer(p1.balance - p2.balance);
    }
    
    int main(int argc, char* argv[])
    {
        Customer c1(100);
        Customer c2(10);
        Customer c3 = c1 - c2;
        cout << c3.balance << endl;
    }
    
    
    打赏 评论

相关推荐 更多相似问题