如何使用重载运算符>在一个功能

时间:2014-11-10 06:33:19

标签: c++

我有一个功能非常好。但是,我需要在其中使用重载运算符,这对我来说并不适用,因为我遇到了错误。我从理论中得到一个小例子,它的写法如下:

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

2 个答案:

答案 0 :(得分:0)

假设您要重载运算符&gt;为Book类。

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

首先,你必须决定你将决定比较Book对象的成员。让我们把名称作为我们将重载运算符的关键&gt;

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

答案 1 :(得分:0)

您正确地重载了​​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)中,您不能在{+ 1}}中使用new作为参数名称,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