1

To keep things simple, this is my class assignment: Add the following functions to the Cspinner class.

Overload the equal sign to compare two spinners. The following instruction should be valid if w1, w2 and w3 are spinners.

if ( w1 == w2 && w2 == w3)     cout << "You win!";

Overload the equal sign to compare a spinner and a fruit (an array of characters). The following instruction should be valid.

if (w1 == "Apple") cout << "Spinner is a Apple.";

Overload the output (<<). This will replace the show procedure. Below is an example.

cout << w1 << endl;

Overload one of the functions inside of the class!!

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

Example output of the above program.

orange apple apple peach banana apple banana apple apple orange orange apple banana cherry apple orange orange orange (All Oranges) orange apple apple peach apple apple orange apple peach apple cherry apple

Here is the code I have, I just can't figure out how to overload to operators, I will appreciate any explaining with this matter. Thank you!

#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");
}
  • Not answer, just a note that you should check the total possibility is always 100. – apple apple Feb 24 '21 at 18:04
  • 1
    Helpful reading: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Feb 24 '21 at 18:04

1 Answers1

2

The main thing that you need to remember here is that when you're overloading a class operator in C++, the left hand side of the argument is the implicit this. That is, the left had side of the argument is this instance of the class. Take for example the following small program.

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

Running this program produces "W1 is smaller."

Now, I am assuming that what you want to compare with the equivalency operator == is the value stored in the Result variable. In this case, the code to overload the operator would be:

bool operator == (const Cspinner &c) const {
    return this->Result == c.Result;
}

What this is doing is saying I want to compare the value stored in the Result variable of this instance of Cspinner, to the value of Result stored in the instance const Cspinner &c

Similarly, to test the equality of an instance of Cspinner to a string, one could overload the equality operator like the following.

bool operator == (const string &s) const {
    return this->Result == s;
}

Since the variable stored in this->Result is of type string, the overloaded == will compare using the equality operator for the string class.

Justin
  • 136
  • 4
  • Thank you very much for your help and explanation. I used what you explained so I can overload cout << w1 the following way: //I had to make this a friend of Cspinner ostream& operator << (ostream& left, Cspinner Right) { left << Right.Result; return left; } – Gerald Marcano Feb 26 '21 at 00:03