用重载运算符C ++

时间:2018-04-04 23:17:04

标签: c++ operators

在我的任务中,我被困在一个领域,知道我阅读过多少次教科书或尝试寻求帮助。我觉得在这个地方真的很困惑,并且可以真正使用一些建议,这样我就可以克服这个障碍并学习什么是正确的。我还是编程新手。所以忍受我。我们有两个.cpp文件和一个.h文件。其中一个.cpp有一个switch语句,根据用户的选择调用不同的函数。在旧的赋值中,它只是调用函数,但我们使用相同的赋值并将其更改为重载的运算符。我们被指示操作.cpp文件以用我们的运算符替换函数调用。但是,我无法弄清楚如何调用这些运算符,以便他们打印出提示并通过运算符而不是旧函数接收用户值。 TIA提供任何帮助。

.h文件

    #ifndef _FRACTION_H_
    #define _FRACTION_H_
    #include "iostream"
    using namespace std;

    class fraction
    {
    private:
        int      numerator;
        int      denominator;

    public:
        fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {};
        friend fraction operator+(fraction f1, fraction f2);
        friend fraction operator-(fraction f1, fraction f2);
        friend fraction operator*(fraction f1, fraction f2);
        friend fraction operator/(fraction f1, fraction f2);
        friend ostream& operator<<(ostream& out, fraction& f);
        friend istream& operator>>(istream& in, fraction& f);
    };

    #endif

.cpp文件

#include <iostream>
#include "fraction.h"
using namespace std;


int gcd(int u, int v);



fraction::fraction(int n, int d) : numerator(n), denominator(d)
{
    int     common = gcd(numerator, denominator);
    numerator /= common;
    denominator /= common;
}



fraction operator+(fraction f1, fraction f2)
{
    int     n = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}



fraction operator-(fraction f1, fraction f2)
{
    int     n = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}


fraction operator*(fraction f1, fraction f2)
{
    int     n = f1.numerator * f2.numerator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}


fraction operator/(fraction f1, fraction f2)
{
    int     n = f1.numerator * f2.denominator;
    int     d = f1.denominator * f2.numerator;

    return fraction(n, d);
}


ostream& operator<<(ostream& out, fraction& f)
{
    out << f.numerator << "/" << f.denominator << endl;

    return out;
}



istream& operator>>(istream& in, fraction& f)
{
    cout << "Please enter the numerator: ";
    cin >> f.numerator;
    cout << "Please enter the denominator: ";
    cin >> f.denominator;

    return in;
}



// Euclid's Algorithm for finding the greatest common divisor

int gcd(int u, int v)
{
    u = (u < 0) ? -u : u;
    v = (v < 0) ? -v : v;

    while (u > 0)
    {
        if (u < v)
        {
            int t = u;  // swap u and v
            u = v;
            v = t;
        }

        u -= v;
    }

    return v;           // the GCD of u and v
}

.cpp文件(这是我需要用运算符替换函数调用的文件) 这是我被困住的地方,不知道如何用我的操作员替换我的函数调用。

#include "fraction.h"
#include <iostream>
using namespace std;

int main()
{
    char        choice;
    fraction    left;
    fraction    right;

    do
    {
        cout << "A\tAdd\n";
        cout << "S\tSub\n";
        cout << "M\tMult\n";
        cout << "D\tDiv\n";
        cout << "E\tExit\n";

        cout << "\nChoice?: ";

        cin >> choice;
        cin.ignore();

        switch (choice)
        {
            case 'A':
            case 'a':
                cout << "Adding" << endl;
                cout << "enter the first operand: ";
                left.read();
                cout << "enter the second operand: ";
                right.read();
                left.add(right).print();
                break;
            case 'S':
            case 's':
                cout << "enter the first operand: ";
                left.read();
                cout << "enter the second operand: ";
                right.read();
                left.sub(right).print();
                break;
            case 'M':
            case 'm':
                cout << "enter the first operand: ";
                left.read();
                cout << "enter the second operand: ";
                right.read();
                left.mult(right).print();
                break;
            case 'D':
            case 'd':
                cout << "enter the first operand: ";
                left.read();
                cout << "enter the second operand: ";
                right.read();
                left.div(right).print();
                break;
            case 'E' :
            case 'e' :
                break;
            default:
                cerr << "Unrecognized choice: " << choice << endl;
                break;
        }
    } while (choice != 'e' && choice != 'E');

    return 0;
}

1 个答案:

答案 0 :(得分:0)

将对read()的使用更改为使用cin >>,将print()替换为cout <<,将add()等方法替换为相应的运算符。< / p>

case 'A':
case 'a':
    cout << "Adding" << endl << "enter the first operand: ";
    cin >> left;
    cout << "enter the second operand: ";
    cin >> right;
    cout << left + right;
    break;

您还应将除>>以外的所有运算符的参数类型更改为const fraction &。这解决了无法找到匹配函数的错误。

这是完整的代码(我只包括A操作,其余的类似):

#include "iostream"
using namespace std;

class fraction
{
private:
    int      numerator;
    int      denominator;

public:
    fraction(int n = 0, int d = 1);
    friend fraction operator+(const fraction &f1, const fraction &f2);
    friend fraction operator-(const fraction &f1, const fraction &f2);
    friend fraction operator*(const fraction &f1, const fraction &f2);
    friend fraction operator/(const fraction &f1, const fraction &f2);
    friend ostream& operator<<(ostream& out, const fraction& f);
    friend istream& operator>>(istream& in, fraction& f);
};

using namespace std;


int gcd(int u, int v);

fraction::fraction(int n, int d) : numerator(n), denominator(d)
{
    int     common = gcd(numerator, denominator);
    numerator /= common;
    denominator /= common;
}

fraction operator+(const fraction &f1, const fraction &f2)
{
    int     n = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}

fraction operator-(const fraction &f1, const fraction &f2)
{
    int     n = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}

fraction operator*(const fraction &f1, const fraction &f2)
{
    int     n = f1.numerator * f2.numerator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}

fraction operator/(const fraction &f1, const fraction &f2)
{
    int     n = f1.numerator * f2.denominator;
    int     d = f1.denominator * f2.numerator;

    return fraction(n, d);
}

ostream& operator<<(ostream& out, const fraction& f)
{
    out << f.numerator << "/" << f.denominator << endl;

    return out;
}



istream& operator>>(istream& in, fraction& f)
{
    cout << "Please enter the numerator: ";
    cin >> f.numerator;
    cout << "Please enter the denominator: ";
    cin >> f.denominator;

    return in;
}



// Euclid's Algorithm for finding the greatest common divisor

int gcd(int u, int v)
{
    u = (u < 0) ? -u : u;
    v = (v < 0) ? -v : v;

    while (u > 0)
    {
        if (u < v)
        {
            int t = u;  // swap u and v
            u = v;
            v = t;
        }

        u -= v;
    }

    return v;           // the GCD of u and v
}

int main()
{
    char        choice;
    fraction    left;
    fraction    right;

    do
    {
        cout << "A\tAdd\n";
        cout << "S\tSub\n";
        cout << "M\tMult\n";
        cout << "D\tDiv\n";
        cout << "E\tExit\n";

        cout << "\nChoice?: ";

        cin >> choice;
        cin.ignore();

        switch (choice)
        {
        case 'A':
        case 'a':
            cout << "Adding" << endl << "enter the first operand: ";
            cin >> left;
            cout << "enter the second operand: ";
            cin >> right;
            cout << left + right;
            break;
        case 'E' :
        case 'e' :
            break;
        default:
            cerr << "Unrecognized choice: " << choice << endl;
            break;
        }
    } while (choice != 'e' && choice != 'E');

    return 0;
}