Code instructions:
The Coin class will act as the base class. Three child classes will be created using the Coin class as the
base class. The classes will be Quarter, Dime, and Nickel. These child classes will contain a constructor
that will be used to set the value field to the value of their amount (Quarter = 0.25, Dime=0.10,
Nickel=0.05)
Instead of creating a separate counter variable to keep the balance of the coins the static field balance will keep track of the balance of the coin tosses. Write a program that uses the Quarter, Dime, and Nickel classes.
The program will create one of each instance of the Quarter, Dime, and Nickel class. When the game begins, your starting balance is $0. During each round of the game, the program will toss each of the simulated coins. When a tossed coin lands heads-up, the value of the coin is added to
your balance. For example, if the quarter lands heads-up, 25 cents is added to your balance. Nothing is added to your balance for coins that land tails-up. The game is over when your balance reaches one
dollar or more. If your balance is exactly one dollar, you win the game. If your balance exceeds one dollar, you lose.
In the main create a function called evaluateToss that will take a Coin reference pointer parameter. Use this method to toss the coin, decide if the coin is showing heads or tails, and add the value to the
balance if needed. Pass the Quarter, Dime, and Nickel objects to this function each time to do the
work.
Below is the code I've written so far but can anyone please help me to write correct code by using pointer reference parameter in evaluateToss function so that it works correctly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
//Coin class
class Coin {
private:
double balance;
string sideUp;
bool heads;
double value;
public:
Coin(double val) {
value = val;
int side = rand() % 2;
if (side == 0) {
sideUp = "heads";
heads = true;
}
else {
sideUp = "tails";
heads = false;
}
}
void toss() {
int side = rand() % 2;
if (side == 0) {
sideUp = "heads";
heads = true;
}
else {
sideUp = "tails";
heads = false;
}
}
void addBalance() {
balance += value;
}
double getBalance() {
return balance;
}
bool getHeads() {
return heads;
}
double getValue() {
return value;
}
string getSideUp() {
return sideUp;
}
};
//Dime class sets value
class Dime : public Coin { public: Dime(double val) : Coin(val) {} };
//Quarter class sets value
class Quarter : public Coin {
public:
Quarter(double value) : Coin(value) {
}
};
//Nickle class sets value
class Nickle : public Coin {
public:
Nickle(double value) : Coin(value) {
}
};
//evaluateToss method
void evaluateToss(Coin &coin) {
coin.toss();
if (coin.getHeads()==true) {
coin.addBalance();
}
else {
return;
}
std::cout << coin.getBalance();
cout << "\n\n";
if (coin.getBalance() == 1.0) {
std::cout << "You win!!!";
}
else if (coin.getBalance() > 1.0) {
std::cout << "You lose!!!";
}
}
//main class
int main()
{
std::cout << "Starting the Coin Toss Game";
std::cout << "Ready? Set? Go!!!";
Coin coin(0);
coin.addBalance();
Dime dime(0.10);
Nickle nickle(0.05);
Quarter quarter(0.25);
while (coin.getBalance()<1.0) {
evaluateToss(dime);
evaluateToss(nickle);
evaluateToss(quarter);
}
}
|