如何在函数中使用重载运算符>

How to use the overloading operator > in a function

本文关键字:运算符 重载 gt 函数      更新时间:2023-10-16

我有一个函数运行得很好。然而,我需要在其中使用重载运算符,这对我来说不起作用,因为我会出错。我有一个理论上的小例子,上面写着:

int MaxPrice(Container & A, string name) // finds an element number with maximum price of a book
{
int max = 0, n = A.GetN();
while (max < n && A[max].GetName() != name)
max++;
if (max == n)
return -1;
Book maxBook = A[max];
for (int i = max; i < n; i++)
if (A[i] > maxBook) {
maxBook = A[i];
max = i;
}
return max;
}

我有我的书面功能:

double MaxPrice(Books & A, string nm) // finds maximum price of a book
{
    double maxPric = 0.0;
    for (int i = 0; i < A.Get(); i++)
    {
        if (A.Get(i).GetName() == pv && A.Get(i).GetPrice() > maxPric)
            maxPric = A.Get(i).GetPrice();
    }
    return maxPric;
}

我的课程是图书和书籍(容器类):

class Book
{
private:
    string publisher;   // book publisher
    string name;        // name of a book
    int quantity;       // quantity of a book
    double price;       // price of one book
public:
    Book(): publisher(""), name(""), quantity(1), price(0.0) { }
    Book(string publish, string nam, int quantit, double pric): 
        publisher(publish), name(nam), quantity(quantit), price(pric) { }
    ~Book() { }
    void Set(string pu, string na, int qu, double pr);
    void SetName(string pu);
    void SetPrice(double pr);
    string GetPublisher() { return publisher; };
    string GetName() { return name; };
    int GetQuantity() { return quantity; };
    double GetPrice() { return price; };

    bool operator > (const Book & next);
};
void Book::Set(string pu, string na, int qu, double pr)
{
    publisher = pu;
    name = na;
    quantity = qu;
    price = pr;
}
void Book::SetName(string na)
{
    name= na;
}
void Book::SetPrice(double pr)
{
    price = pr;
}
bool Book::operator > (const Book & next)
{
    return (price > next.price);
}
//----------------------

class Books
{
public:
    static const int Cn = 100;  // maximum number of books
private:
    Book K[Cn]; // books data
    int n;          // quantity of books
public:
    Books(): n(0) { }
    ~Books() { }
    int Get() // returns quantity of books
    { return n; };  
        void Set(Book new)  // add a new book to array of books 
    { K[n++] = new; }       // and pluses one to quantity of books
    Book Get(int ind)   // returns object by index
    { return K[ind]; }
    double Sum();
};
double Books::Sum()
{
    double sum = 0;
    for (int i = 0; i < n; i++)
        sum += K[i].GetPrice();
    return sum;
}

假设您想为Book类重载operator>。

class Book
{
private:
    string publisher;   // book publisher
    string name;        // name of a book
    int quantity;       // quantity of a book
    double price; 

首先,您必须决定由哪个成员决定Book对象的比较。让我们把name作为重载operator>的键。

bool operator> ( const Book& book )
{
  return name > book.name ;
}

您正确地重载了operator >。在Book中加载运算符后,可以像book1 > book2一样使用它
我将您的MaxPrice()更改为:

double MaxPrice(Books & A) // finds maximum price of a book
{
    if(A.Get() == 0) return -1;
    Book curExpensiveBook = A.Get(0);
    for (int i = 1; i < A.Get(); i++)
    {
        if(A.Get(i) > curExpensiveBook) //Compare directly by using >
            curExpensiveBook = A.Get(i);
    }
    return curExpensiveBook.GetPrice();
}

但您的代码中还有其他问题:在Books::Set(Book)中,不能使用new作为C++中的参数名称,new是C++中的保留关键字:

void Set(Book new)  // add a new book to array of books 
    { K[n++] = new; }

更改为另一个名称:

void Set(Book newBook)  // add a new book to array of books 
        { K[n++] = newBook; }

以下是您的代码的工作演示:http://ideone.com/XYJBA2