C++面向对象入门(二十一)运算符重载

运算符重载: 对已有的运算符进行重新定义, 赋予新的功能, 以适应不同的数据类型(多指自定义数据类型)
重载方式:
1,类成员函数
2,全局函数
一般的全局函数进行运算符重载要比类成员函数运算符重载多一个参数,
因为往往使用非静态成员函数进行运算符重载, 而非静态成员函数隐含的的this指针指向的对象, 往往是全局函数第一个对象参数, 故可省略
类成员函数进行运算符重载语法:
类名 operator运算符(参数列表) {
函数体
}

全局函数进行运算符重载语法:
类名 operator运算符(参数列表) {
函数体
}

代码示例:
#include <iostream>
#include <string>

using namespace std;

const int MAXSIZE = 100;

/**
 * 运算符重载: 对已有的运算符进行重新定义, 赋予新的功能, 以适应不同的数据类型(多指自定义数据类型)
 * 重载方式:
 * 1,类成员函数
 * 2,全局函数
 * 一般的全局函数进行运算符重载要比类成员函数运算符重载多一个参数,
 * 因为往往使用非静态成员函数进行运算符重载, 而非静态成员函数隐含的的this指针指向的对象, 往往是全局函数第一个对象参数, 故可省略
 * 类成员函数进行运算符重载语法:
 * 类名 operator运算符(参数列表) {
 * 函数体
 * }
 *
 * 全局函数进行运算符重载语法:
 * 类名 operator运算符(参数列表) {
 * 函数体
 * }
 */
class Bill;

class Item {
    friend class Bill;

    friend Bill operator+(const Bill &bill1, const Bill &bill2);
private:
    string name;
    double price;
    int quantity;
    int number;
public:
    Item() {
        name = "--";
        price = 0.0;
        quantity = 0;
        number = 0;
    }

    Item(const string &name, double price, int quantity, int number) : name(name), price(price), quantity(quantity),
                                                                       number(number) {}

    string getInfo() const {
        return "Item{name:" + name + ", price:" + to_string(price) + ", quantity:" + to_string(quantity) +
               "} totalprice:" + to_string(price * quantity);
    }
};

struct Goods {
    string name;
    double price;

    string getInfo() {
        return "name: " + name + ", price: " + to_string(price);
    }
};

class Bill {
    friend Bill operator+(const Bill &bill1, const Bill &bill2);
private:
    Item items[MAXSIZE];
    double totalPrice;
public:
    Bill() {
        totalPrice = 0;
    }

    Bill(Item *items, int itemQuantity) {
        totalPrice = 0;
        for (int i = 0; i < itemQuantity; ++i) {
            this->items[items[i].number] = items[i];
            totalPrice += items[i].price * items[i].quantity;
        }
    }

    double getTotalPrice() const;

    void showBill() {
        for (int i = 0; i < MAXSIZE; ++i) {
            if (items[i].quantity) {
                cout << items[i].getInfo() << endl;
            }
        }
    }

    //使用成员函数重载+运算符
    /*Bill operator+(const Bill &bill) ;*/
};

/**
 * 实现加运算
 * @param bill 加账单
 * @return 两个账单之和
 */
/*Bill Bill::operator+(const Bill &bill) {
    Bill tb;
    tb.totalPrice = totalPrice + bill.totalPrice;
    for (int i = 0; i < MAXSIZE; ++i) {
        tb.items[i] = items[i];
        tb.items[i].quantity = items[i].quantity + bill.items[i].quantity;
    }
    return tb;
}*/

/**
 * 使用友元全局函数重载Bill类的+号运算符
 * @param bill1 被加Bill类对象
 * @param bill2 加Bill类对象
 * @return 两个Bill类对象之和
 */
Bill operator+(const Bill &bill1, const Bill &bill2) {
    Bill tb;
    tb.totalPrice = bill1.totalPrice + bill2.totalPrice;
    for (int i = 0; i < MAXSIZE; ++i) {
        tb.items[i] = bill1.items[i];
        tb.items[i].quantity = bill1.items[i].quantity + bill2.items[i].quantity;
    }
    return tb;
}

double Bill::getTotalPrice() const {
    return totalPrice;
}

void test1() {
    Item items1[] = {Item("apple", 2.5, 10, 1), Item("watermelon", 1.2, 100, 3), Item("grape", 10, 15, 2)};
    Bill bill1(items1, 3);
    Item items2[] = {Item("apple", 2.5, 15, 1), Item("grape", 10, 15, 2)};
    Bill bill2(items2, 2);
    Bill bill3 = bill1 + bill2;
    bill1.showBill();
    bill2.showBill();
    bill3.showBill();
    cout << bill1.getTotalPrice() << endl;
    cout << bill2.getTotalPrice() << endl;
    cout << bill3.getTotalPrice() << endl;
}

int main() {
    test1();
    system("pause");

    return 0;
}

 

 
posted @ 2020-08-20 18:18  DNoSay  阅读(175)  评论(0编辑  收藏  举报