繁体   English   中英

C ++运算符重载始终为false

[英]C++ Operator Overloading always false

因此,我能够对其进行修复,但是由于我总是会出错,因此操作员似乎没有对它们进行比较。 pLoan似乎有一个错误,它没有将它们两者都进行比较。 我的代码是

    #include <string>
    #include <iostream>
    #include <sstream>
    using namespace std;

    //Vehicle Class
    class Vehicle {
    public:
        Vehicle();
        void setPrice(double a);
        void setMpg(int a);
        double getPrice() const;
        int getMpg() const;
        void printVehicle() const;
        Vehicle(double price, int mpg);
    private:
        double price;
        int mpg;
    };

    //Loan Class
    class Loan {
    public:
        void setBank(string a);
        void setLoan(double a);
        string getBank() const;
        double getLoan() const;
        void printLoan() const;
        Loan(string bank = "", double loan = 0);
    private:
        string bank;
        double loan;
    };

    //Car Class
    class Car : public Vehicle {
    public:
        Car(double price = 0, int mpg = 0, string bank = "", double loan = 0, string name = "", int element = 0);
        void setName(string a);
        void setLoan(string b, double l, int element);
        string getName() const;
        void printFull() const;
        void setNbrOfLoans(int a);
        int getNbrOfLoans() const;
        Loan* getpLoan() const;
        ~Car();
    private:
        string name;
        Loan* pLoan;
        int nbrOfLoans;
    };

    bool operator==(const Car &car1, const Car &car2) {
        Loan* pLoan1 = car1.getpLoan();
        Loan* pLoan2 = car2.getpLoan();
        return ((car1.getPrice() == car2.getPrice()) && (car1.getMpg() == car2.getMpg()) && (car1.getName() == car2.getName())
            && (car1.getNbrOfLoans() == car2.getNbrOfLoans()) && 
            (pLoan1[0].getBank() == pLoan2[0].getBank()) && (pLoan1[0].getLoan() == pLoan2[0].getLoan()));
    }

    //Main
    int main() {
        Car car1(24800, 22, "Citi", 21600, "Mustang", 1);
        Car* pCar1 = &car1;
        pCar1->setLoan("Citi", 21600, 0);
        pCar1->printFull();
        pCar1->setNbrOfLoans(1);
        Car car2;
        Car* pCar2 = &car2;

        cout << boolalpha;
        cout << "Enter the price of the car: ";
        double price;
        cin >> price;
        pCar2->setPrice(price);
        cout << "Enter the mpg: ";
        int mpg;
        cin >> mpg;
        pCar2->setMpg(mpg);
        cout << "Enter the name of the car: ";
        string name;
        cin >> name;
        pCar2->setName(name);
        string bank;
        double loan;
        int index;
        cout << "Enter the amount of loans you obtained: ";
        cin >> index;
        pCar2->setNbrOfLoans(index);
        for (int i = 0; i < index; i++)
        {
            cout << "Enter the name of bank " << i + 1 << ": ";
            cin >> bank;
            cout << "Enter the amount of loan " << i + 1 << ": ";
            cin >> loan;
            pCar2->setLoan(bank, loan, i);
        }
        if (pCar1 == pCar2)
            cout << "Cars are the same. ";
        else
            cout << "Cars are not the same. ";
        cout << endl;
        pCar2->printFull();
        return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////////////
    //Vehicle class function definitions
    ////////////////////////////////////////////////////////////////////////////////////////
    Vehicle::Vehicle() {
        price = 0;
        mpg = 0;
    }

    Vehicle::Vehicle(double price, int mpg) {
        price = price;
        mpg = mpg;
    }

    void Vehicle::setPrice(double a) {
        price = a;
    }

    void Vehicle::setMpg(int a) {
        mpg = a;
    }

    double Vehicle::getPrice() const {
        return price;
    }

    int Vehicle::getMpg() const {
        return mpg;
    }

    void Vehicle::printVehicle() const {
        cout << "Price: " << price << endl;
        cout << "MPG: " << mpg << endl;
    }

    ////////////////////////////////////////////////////////////////////////////////////////
    //Loan Class function definitions
    ///////////////////////////////////////////////////////////////////////////////////////

    Loan::Loan(string bank, double loan) {
        Loan::bank = bank;
        Loan::loan = loan;
    }

    void Loan::setBank(string a) {
        bank = a;
    }

    void Loan::setLoan(double a) {
        loan = a;
    }

    string Loan::getBank() const {
        return bank;
    }

    double Loan::getLoan() const {
        return loan;
    }
    void Loan::printLoan() const {
        cout << "Bank: " << bank << endl;
        cout << "Loan: " << loan << endl;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    //Car Class function definitions
    ////////////////////////////////////////////////////////////////////////////////////
    Car::Car(double price, int mpg, string bank, double loan, string name, int element) : Vehicle(price, mpg)
    {
        nbrOfLoans = element;
        Car::name = name;
        setMpg(mpg);
        setPrice(price);
        pLoan = new Loan[nbrOfLoans];
    }

    Loan* Car::getpLoan() const{
        return pLoan;
    }

    void Car::setName(string a) {
        name = a;
    }

    void Car::setLoan(string b, double l, int element) {
        pLoan[element].setBank(b);
        pLoan[element].setLoan(l);
    }

    string Car::getName() const {
        return name;
    }

    int Car::getNbrOfLoans() const {
        return nbrOfLoans;
    }

    void Car::setNbrOfLoans(int a) {
        nbrOfLoans = a;
        if (pLoan != NULL)
            delete[] pLoan;
        pLoan = new Loan[nbrOfLoans];
    }

    void Car::printFull() const {
        cout << endl << "Car name: " << name << endl;
        cout << "Price: " << getPrice() << endl;
        cout << "MPG: " << getMpg() << endl;
        for (int i = 0; i < nbrOfLoans; i++)
        {
            cout << "Loan #" << i + 1 << "." << endl;
            cout << "Bank: " << pLoan[i].getBank();
            cout << endl;
            cout << "Loan amount: " << pLoan[i].getLoan();
            cout << endl;
        }
    }

    Car::~Car() {
        delete[] pLoan;
    }

输出:IS总是汽车不一样,即使它们是

您的主要代码未调用您的operator ==

    if (pCar1 == pCar2)
        cout << "Cars are the same. ";
    else
        cout << "Cars are not the same. ";

在这里,您正在比较两个指针 比较您需要的两个指向对象

    if (*pCar1 == *pCar2) ...

还有一个错误:

pCar1->setLoan("Citi", 21600, 0);
pCar1->printFull();
pCar1->setNbrOfLoans(1);

setNbrOfLoans必须位于setLoan之前:

pCar1->setNbrOfLoans(1);
pCar1->setLoan("Citi", 21600, 0);
pCar1->printFull();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM