如何在函数中使用重载运算符>
How to use the overloading operator > in a function
我有一个函数运行得很好。然而,我需要在其中使用重载运算符,这对我来说不起作用,因为我会出错。我有一个理论上的小例子,上面写着:
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
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- <T> 通过模板化运算符重载将 std::complex 乘以双倍
- C++20概念:需要运算符重载
- 使用赋值运算符重载从类中返回jobject
- 在运算符重载定义中使用成员函数(const错误)
- 字节到位运算符重载C++
- 为什么在运算符重载时需要参考?
- 类中 c++ 的运算符 + 重载
- 算术复合运算符重载为非成员
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 交换运算符 + 重载会导致无限递归
- 如何理解新的运算符重载?
- 向量保持复数的运算符重载
- 如何创建运算符重载?
- 链接列表运算符重载没有打印出我想要的内容
- C++:需要帮助了解运算符重载错误
- 使用模板化运算符重载 XOR 运算符失败
- 如何确保接受的C++模板类型使运算符重载?
- 运算符重载使用运算符+添加类模板