使用类来创建抛硬币程序

时间:2015-04-23 03:09:47

标签: c++

无法搞清楚构造函数,main()中的代码应该测试类,以便我可以继续执行程序的其余部分。 这是说明 构造

这个类有两个构造函数。默认构造函数(不带参数的构造函数)应该将硬币的值初始化为便士(0.01),并且应该通过调用下面描述的toss()方法初始化该方。

另一个构造函数接受一个参数:一个保存硬币初始值的double。传入的参数应该用于初始化硬币的值。不需要进行错误检查。应该通过调用下面描述的toss()方法初始化该方。

这是代码

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


    }

1 个答案:

答案 0 :(得分:0)

终于想出来了,感谢任何努力的人!

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



}