help constructors for classes
Apr 23, 2015 at 4:04am UTC
aving trouble figuring out the constructors the code in the main() is supposed to test the class so i can go on to the rest of the program. this is the instructions Constructors
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the value of the coin to a penny (0.01) and the side should be initialized by calling the toss() method that is described below.
The other constructor takes 1 argument: a double that holds the initial value for the coin. The passed in argument should be used to initialize the value of the coin. No error checking is required. The side should be initialized by calling the toss() method that is described below.
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
//Put the Coin class definition after this line
class Coin
{
public :
Coin()
{
value = 0.01;
toss();
}
Coin(double )
{
toss();
}
void toss();
int getSide();
double getValue();
private :
double value;
char side[6];
};
int main()
{
//Set the random number generator and the formatting for the output
srand( 1 );
cout << fixed << setprecision(2);
//Create objects using the two constructors
Coin coin1;
Coin coin2( 0.25 );
Coin coin3( 0.10 );
//Test 1: the getSide method
cout << "Test 1: use the getSide method on all of the objects" << endl << endl
<< "coin1 has " << ( (coin1.getSide() == 1)? "heads" : "tails" ) << " up" << endl
<< "coin2 has " << ( (coin2.getSide() == 1)? "heads" : "tails" ) << " up" << endl
<< "coin3 has " << ( (coin3.getSide() == 1)? "heads" : "tails" ) << " up" << endl;
//Test 2: the getValue method
cout << endl << endl
<< "Test 2: use the getValue method on all of the objects" << endl << endl
<< "coin1 has the value $" << coin1.getValue() << endl
<< "coin2 has the value $" << coin2.getValue() << endl
<< "coin3 has the value $" << coin3.getValue() << endl;
//Test 3: the toss method
cout << endl << endl
<< "Test 3: use the toss method 5 times on all of the objects" << endl << endl;
cout << "coin1 coin2 coin3" << endl
<< "-----------------------------" << endl;
for ( int cnt = 1; cnt <= 5; cnt++ )
{
coin1.toss();
cout << ( (coin1.getSide() == 1)? "heads" : "tails" );
coin2.toss();
cout << ( (coin2.getSide() == 1)? " heads" : " tails" );
coin3.toss();
cout << ( (coin3.getSide() == 1)? " heads" : " tails" ) << endl;
}
return 0;
}
//Code the constructors and methods for the Coin class after this line
void Coin::toss()
{
int num = rand() % 2+ 1;
if (num == 1)
{
strcpy(side,"heads" );
}
else
{
strcpy(side,"tails" );
}
}
int Coin::getSide()
{
if (side=="heads" )
{
return 1;
}
else
{
return 2;
}
}
double Coin::getValue()
{
return value;
}
Last edited on Apr 23, 2015 at 11:15pm UTC
Apr 23, 2015 at 8:08am UTC
You may write the constructors like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Coin
{
public :
Coin()
{
value = 0.01;
toss();
}
Coin(double v) : value(v)
{
toss();
}
C++11:
1 2 3 4 5 6 7 8 9 10
class Coin
{
public :
Coin(double v) : value(v)
{
toss();
}
Coin() : Coin(0.01)
{
}
Two constructors are unnecessary:
1 2 3 4 5 6 7
class Coin
{
public :
Coin(double v = 0.01) : value(v)
{
toss();
}
Apr 23, 2015 at 11:14pm UTC
thanks alot for you help i figured it out heres the final code FOR ANYONE WHOSE INTERESTED.
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
//Put the Coin class definition after this line
class Coin
{
public :
Coin();
Coin(double );
void toss();
int getSide();
double getValue();
private :
double value;
char side[6];
};
int main()
{
//Set the random number generator and the formatting for the output
srand( time(NULL) );
cout << fixed << setprecision(2);
//Create objects using the two constructors
Coin coin1;
Coin coin2( 0.25 );
Coin coin3( 0.10 );
double balance;
while (balance < 1.00)
{
cout << "current Balance: " <<balance<<endl;
coin1.toss();
cout<< "Nickel: " << ( (coin1.getSide() == 1)? "heads" : "tails" )<<endl;
if (coin1.getSide()==1)
{
balance +=coin1.getValue();
}
coin3.toss();
cout<< "Dime: " << ( (coin3.getSide() == 1)? "heads" : "tails" )<<endl;
if (coin3.getSide()==1)
{
balance +=coin3.getValue();
}
coin2.toss();
cout<< "Quarter: " << ( (coin2.getSide() == 1)? "heads" : "tails" )<<endl;
if (coin2.getSide()==1)
{
balance +=coin2.getValue();
}
cout << "-----------------------------" <<endl
<< "your current balance is $" << balance <<endl
<< endl;
}
cout<< "The final balance is " << balance<<" Congrats! You won!" <<endl;
return 0;
}
//Code the constructors and methods for the Coin class after this line
void Coin::toss()
{
int num = rand() % 2+ 1;
if (num == 1)
{
strcpy(side,"heads" );
}
else
{
strcpy(side,"tails" );
}
}
int Coin::getSide()
{
if (strcmp(side,"heads" )==true )
{
return 1;
}
else
return 2;
}
double Coin::getValue()
{
return value;
}
Coin::Coin()
{
value = 0.05;
toss();
}
Coin::Coin(double newValue)
{
value = newValue;
}
Topic archived. No new replies allowed.