[英]How can I overload these operators to make this program work?
為簡單起見,這是我的 class 分配:將以下函數添加到 Cspinner class。
重載等號以比較兩個微調器。 如果 w1、w2 和 w3 是微調器,則以下指令應該是有效的。
if ( w1 == w2 && w2 == w3) cout << "You win!";
重載等號以比較微調器和水果(字符數組)。 以下說明應該是有效的。
if (w1 == "Apple") cout << "Spinner is a Apple.";
重載 output (<<)。 這將取代顯示程序。 下面是一個例子。
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;
}
}
上述程序的示例 output。
橙子 蘋果 蘋果 桃子 香蕉蘋果 香蕉蘋果 蘋果 橙子 蘋果 香蕉櫻桃 蘋果 橙子 橙子 橙子 (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++ 中重載 class 運算符時,參數的左側是隱含的 this。 也就是說,參數的左邊是 class 的這個實例。 以下面這個小程序為例。
#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變量中的值與存儲在實例const Cspinner &c
中的Result的值進行比較
類似地,要測試 Cspinner 實例與字符串的相等性,可以重載相等運算符,如下所示。
bool operator == (const string &s) const {
return this->Result == s;
}
由於存儲在this->Result
中的變量是字符串類型,重載的==
將使用字符串 class 的相等運算符進行比較。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.