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