C++ 编程 运算符重载

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

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

 // 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;
}

另一个方式:

 // 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;
}