C++ coin game using inheritance

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);
		}
		
	
}
Before getting started a US 5-cent coin is a Nickel, not a Nickle
https://en.wikipedia.org/wiki/Nickel_(United_States_coin)
PS A nickel is on the way to being worth about 4 billion rubles by the end of next week.
Do you mean something like this:

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;
//Coin class
class Coin {
private:
	double balance {};
	string sideUp;
	bool heads {};
	double value {};

public:
	Coin(double val = 0) : value(val), balance(val) {
		toss();
	}

	void toss() {
		if (rand() % 2 == 0) {
			sideUp = "heads";
			heads = true;
		} else {
			sideUp = "tails";
			heads = false;
		}
	}

	void addBalance() {
		balance += value;
	}

	double getBalance() const {
		return balance;
	}

	bool getHeads() const {
		return heads;
	}

	double getValue() const {
		return value;
	}

	string getSideUp() const {
		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
double evaluateToss(Coin& coin) {
	coin.toss();

	if (coin.getHeads()) {
		coin.addBalance();
		//std::cout << coin.getBalance();
		//cout << "\n\n";
	}

	return coin.getBalance();
}

//main class
int main() {
	srand(static_cast<unsigned int>(time(nullptr)));

	std::cout << "Starting the Coin Toss Game";
	std::cout << "Ready? Set? Go!!!\n\n";

	Dime dime(0.10);
	Nickle nickle(0.05);
	Quarter quarter(0.25);

	double total {};

	while ((total = evaluateToss(dime) + evaluateToss(nickle) + evaluateToss(quarter)) < 1);

	if (total > 1)
		std::cout << total << " You loose\n";
	else
		std::cout << total << " You win\n";
}


Note that dime, nickle and quarter are separate variables so the win test has to outside of the class if the win considers all of these.
Last edited on
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
#include <iostream>
#include <string>
#include <ctime>

class Coin
{
protected:
    int value;
    int balance{};
    
public:
    int getValue(){ return value;}
    int getBalance(){ return balance;}
    void addBalance(int amount){ balance += amount;}
    
    char flipCoin()
    {
        if (rand() % 2 == 1)
            return 'H';
        else
            return 'T';
    }
};

class Nickel : public Coin{ public: Nickel(){value = 5;} };
class Dime : public Coin {public: Dime(){value = 10;} };
class Quarter : public Coin {public: Quarter(){value = 25;} };

char evaluateToss(Coin* coin)
{
    char face = coin->flipCoin();
    if( face == 'H' )
        coin->addBalance( coin->getValue() );
    
    return face;
}

int main()
{
    srand (time(nullptr));
    
    std::cout << "Starting the Coin Toss Game";
    std::cout << "Ready? Set? Go!!!\n";
    
    Dime dime;
    Nickel nickel;
    Quarter quarter;
    
    for(int i = 0; i < 3; i++) // TEST WITH # TURNS
    {
        // A SINGLE TURN:
        std::cout
        << dime.getValue() << " cents "<< evaluateToss(&dime) << '\n'
        << nickel.getValue() << " cents "<< evaluateToss(&nickel) << '\n'
        << quarter.getValue() << " cents "<< evaluateToss(&quarter) << '\n';
        
        std::cout
        << "Total balance: $"
        << (dime.getBalance() + nickel.getBalance() + quarter.getBalance())/100.
        << "\n\n";
    }
    
    return 0;
}


Starting the Coin Toss GameReady? Set? Go!!!
10 cents T
5 cents H
25 cents T
Total balance: $0.05

10 cents H
5 cents T
25 cents T
Total balance: $0.15

10 cents T
5 cents T
25 cents T
Total balance: $0.15

Program ended with exit code: 0
Topic archived. No new replies allowed.