在没有参数

时间:2016-02-23 06:25:43

标签: c++ constructor

我正在尝试使用名为PowerballTicketPowerballLottery的类创建一个Powerball模拟器。对于PowerballTicket,如果在没有参数的情况下调用构造函数,则需要生成6个随机数(如真正的Powerball中的quickpick选项)。到目前为止我所拥有的是

PowerballTicket::PowerballTicket(int x1, int x2, int x3, int x4, int x5, int x6)
{
    RandomNumber randNum (1, 69);
    x1 = randNum.random();
    do {
        x2 = randNum.random();
    }while (x1 == x2);
    do {
        x3 = randNum.random();
    }while (x3 == x1 || x3 == x2);
    do {
        x4 = randNum.random();
    }while (x4 == x1 || x4 == x2 || x4 == x3);
    do {
        x5 = randNum.random();
    }while (x5 == x1 || x5 == x2 || x5 == x3 || x5 == x4);
    RandomNumber randNumPower (1, 23);
    mpowerball = randNumPower.random();
    mball1 = x1;
    mball2 = x2;
    mball3 = x3;
    mball4 = x4;
    mball5 = x5;
    mpowerball = x6;
}

int PowerballTicket::getball1()
{
    return mball1;
}

int PowerballTicket::getball2()
{
    return mball2;
}

int PowerballTicket::getball3()
{
    return mball3;
}

int PowerballTicket::getball4()
{
    return mball4;
}

int PowerballTicket::getball5()
{
    return mball5;
}

int PowerballTicket::getpowerball()
{
    return mpowerball;
}

我不确定这是否是拥有6个有效随机数的票证以及每个票证的访问方法的正确方法,而且我也不确定如何在有效参数之间切换在构造函数中调用,并且在构造函数中没有调用任何参数。

RandomNumber是一个可以生成随机数的类,我肯定它是有效的。

2 个答案:

答案 0 :(得分:0)

  

我不确定这是否是拥有6个有效随机数的票证以及每个票证的访问方法的正确方法,而且我也不确定如何在构造函数中调用有效参数时切换并且没有在构造函数中调用的参数。

最简单的方法是通过两个构造函数来区分这两个条件。使用默认构造函数从生成的随机数构造票证,使用非默认构造函数使用用户提供的6个数字。

到目前为止,您所拥有的内容无法使用数字进行调用。提供的构造函数不能用作默认构造函数。如果为x1x6提供了默认参数(例如在类定义中),那么它可以,但代码没有提示。

原始代码会覆盖提供的数字,这样做没什么意义,要么应该使用数字,要么应该生成它们。

// default constructor...
PowerballTicket::PowerballTicket()
: mball1(0), mball2(0), mball3(0), mball4(0), mball5(0), mpowerball(0)
// you could exclude the above initialisations, the will be written to below
{
    // generate the random numbers
    RandomNumber randNum (1, 69);
    mball1 = randNum.random();
    do {
        mball2 = randNum.random();
    }while (mball1 == mball2);
    do {
        mball3 = randNum.random();
    }while (mball3 == mball1 || mball3 == mball2);
    do {
        mball4 = randNum.random();
    }while (mball4 == mball1 || mball4 == mball2 || mball4 == mball3);
    do {
        mball5 = randNum.random();
    }while (mball5 == mball1 || mball5 == mball2 || mball5 == mball3 || mball5 == mball4);
    RandomNumber randNumPower (1, 23);
    mpowerball = randNumPower.random();
}

PowerballTicket::PowerballTicket(int x1, int x2, int x3, int x4, int x5, int x6)
: mball1(x1), mball2(x2), mball3(x3), mball4(x4), mball5(x5), mpowerball(x6)
{
    // use the numbers provided
}

默认构造函数生成数字,非默认使用提供的数字作为对象成员。

使用代码;

PowerballTicket myNumbers(1, 2, 3, 4, 5, 6); // non-default constructor
PowerballTicket generatedNumbers; // default constructor

答案 1 :(得分:-1)

创建两个构造函数1参数化和1个空

如果调用空构造函数,则创建6个随机数并调用参数化构造函数

PowerballTicket::PowerballTicket()
{
    RandomNumber randNum (1, 69);
    x1 = randNum.random();
    do {
        x2 = randNum.random();
    }while (x1 == x2);
    do {
        x3 = randNum.random();
    }while (x3 == x1 || x3 == x2);
    do {
        x4 = randNum.random();
    }while (x4 == x1 || x4 == x2 || x4 == x3);
    do {
        x5 = randNum.random();
    }while (x5 == x1 || x5 == x2 || x5 == x3 || x5 == x4);
    RandomNumber randNumPower (1, 23);
    mpowerball = randNumPower.random();
    PowerballTicket(x1,x2,x3,x4,x5, mpoerBall);
}

PowerballTicket::PowerballTicket(int x1, int x2, int x3, int x4, int x5, int x6)
{
 //constructor already have value do what you want
    mball1 = x1;
    mball2 = x2;
    mball3 = x3;
    mball4 = x4;
    mball5 = x5;
    mpowerball = x6;

}