c++ - 如何重载这些运算符以使该程序正常工作?
问题描述
为简单起见,这是我的课堂作业:将以下函数添加到 Cspinner 类中。
重载等号以比较两个微调器。如果 w1、w2 和 w3 是微调器,则以下指令应该是有效的。
if ( w1 == w2 && w2 == w3) cout << "You win!";
重载等号以比较微调器和水果(字符数组)。以下说明应该是有效的。
if (w1 == "Apple") cout << "Spinner is a Apple.";
重载输出 (<<)。这将取代显示程序。下面是一个例子。
cout << w1 << endl;
重载类内部的函数之一!!
void main()
{
Cspinner w1;
Cspinner w2;
Cspinner w3(80,5,5,5,5);
for (int x=0;x<=9;x++)
{
w1.spin();
w2.spin();
w3.spin();
cout << w1 << " " << w2 << " " << w3;
if (w1 == w2 && w2 == w3)
{
if (w1 == "Apple") cout << " (All Apples) ";
else if (w1== "Orange") cout << " (All Oranges) ";
else if (w1== "Cherry") cout << " (All Cherries) ";
else if (w1== "Banana") cout << " (All Bananas) ";
else cout << " (All Peaches)";
}
cout << endl;
}
}
上述程序的示例输出。
橙子 苹果 苹果 桃子 香蕉苹果 香蕉苹果 苹果 橙子 苹果 香蕉樱桃 苹果 橙子 橙子 橙子 (All Oranges)
这是我拥有的代码,我只是不知道如何重载到运营商,我将不胜感激对此事的任何解释。谢谢!
#include <iostream>
using namespace std;
class Cspinner
{
friend void operator << (ostream& left, Cspinner Result);
int apple;
int orange;
int cherry;
int banana;
int peach;
string Result = "Not rolled! ";
public:
Cspinner()
{
apple = 30;
orange = apple + 25;
cherry = orange + 20;
banana = cherry + 15;
peach = banana + 10;
}
Cspinner(int AppleChance, int OrangeChance, int CherryChance, int BananaChance, int PeachChance)
{
apple = AppleChance;
orange = apple + OrangeChance;
cherry = orange + CherryChance;
banana = cherry + BananaChance;
peach = banana + PeachChance;
}
void spin()
{
int RandomNumber = (rand() % 100) + 1;
if (RandomNumber <= apple)
{
Result = "Apple ";
}
else if (RandomNumber <= orange)
{
Result = "Orange ";
}
else if (RandomNumber <= cherry)
{
Result = "Cherry ";
}
else if (RandomNumber <= banana)
{
Result = "Banana ";
}
else if (RandomNumber <= peach)
{
Result = "Peach ";
}
}
bool operator == (char fruit) {
return fruit;
}
bool operator == (Cspinner right) {
return Result == right && left == right;
}
};
//void operator << (ostream left, Cspinner Right) {
// return left, Right;
//}
void main()
{
srand(time(NULL));
Cspinner w1;
Cspinner w2;
Cspinner w3(80, 5, 5, 5, 5);
for (int x = 0;x <= 9;x++)
{
w1.spin();
w2.spin();
w3.spin();
cout << w1 << " " << w2 << " " << w3;
if (w1 == w2 && w2 == w3)
{
if (w1 == "Apple") cout << " (All Apples) ";
else if (w1 == "Orange") cout << " (All Oranges) ";
else if (w1 == "Cherry") cout << " (All Cherries) ";
else if (w1 == "Banana") cout << " (All Bananas) ";
else cout << " (All Peaches)";
}
cout << endl;
}
system("pause");
}
解决方案
您需要记住的主要内容是,当您在 C++ 中重载类运算符时,参数的左侧是隐含的 this。也就是说,参数的左边是这个类的实例。以下面这个小程序为例。
#include <iostream>
using namespace std;
class Wrapper {
public:
int someVal;
Wrapper() { this->someVal = 0; }
Wrapper(int someVal) { this->someVal = someVal;}
bool operator < (const Wrapper &w) const {
return this->someVal < w.someVal;
}
};
int main() {
Wrapper w1;
Wrapper w2(42);
if (w1 < w2) {
cout << "W1 is smaller." << endl;
} else {
cout << "W2 is smaller." << endl;
}
}
运行此程序会产生“W1 更小”。
现在,我假设您要与等价运算符比较的==
是存储在Result变量中的值。在这种情况下,重载运算符的代码将是:
bool operator == (const Cspinner &c) const {
return this->Result == c.Result;
}
这是说我想将存储在 Cspinner 实例的Result变量中的值与存储在实例中的Result值进行比较const Cspinner &c
类似地,要测试 Cspinner 实例与字符串的相等性,可以重载相等运算符,如下所示。
bool operator == (const string &s) const {
return this->Result == s;
}
由于存储的变量this->Result
是字符串类型,因此重载的变量==
将使用字符串类的相等运算符进行比较。
推荐阅读
- vb.net - 从 ADODB 记录集转换为 ADO.net 并且难以找出问题所在
- python - python-click 读取参数不正确(将一个单词拆分为字母)
- c# - 如何在 UWP 中的某些操作上显示简单的信息性消息几秒钟
- asp.net-mvc - Bootstrap 不适用于我的新视图,但它适用于我的第一个视图。(MVC 5)
- reactjs - 在 useEffect API 调用之前调用 useState 变量
- python - 使用 PySimpleGui 单击按钮时,我无法在 gui 上显示按钮名称或保存输入
- reactjs - 无法在 github 页面上重定向 React 路由
- python - 从 ARIMA 中提取汇总值
- azure - 复制 Azure Analysis Services 模型以用于新的数据库源
- ruby-on-rails - 如何解决“Trello::Error: label not found”?