aRandomStudent
aRandomStudent

Reputation: 101

Call one class function in another class

I'm having troubles with classes. I have a class that generates random numbers, and I want to use this random number generator in another class to create a Powerball simulator.

This is my .cpp file for the random number generator:

#include "RandomNumber.h"

#include <random>
#include <utility>

using namespace std;

RandomNumber::RandomNumber( int min, int max,
                           bool minInclusive, bool maxInclusive )
: mMinimum( min ), mMaximum( max )
{
    if (mMinimum > mMaximum)
    {
        swap( mMinimum, mMaximum );
    }
    if (!minInclusive)
    {
        mMinimum++;
    }
    if (!maxInclusive)
    {
        mMaximum--;
    }
}

int RandomNumber::random( )
{
    static random_device rd;
    static mt19937 generator(rd());
    uniform_int_distribution<> distro( mMinimum, mMaximum );

    return( distro( generator ) );
}

This is my header file for the random number generator:

#ifndef RandomNumber_h
#define RandomNumber_h


#include <stdio.h>


class RandomNumber
{
public:
    RandomNumber( int min, int max, bool minInclusive = true, bool maxInclusive= true );

    // supply a number between min and max inclusive
    int random( );
private:
    int  mMinimum, mMaximum;
};

#endif /* RandomNumbers_h */

I wanted to call the member function in another class called PowerballLottery and store the 6 values if they're within the appropriate range, I tried to use

RandomNumber.random( 1, 69 )

and

RandomNumber::random( 1, 69 )

but neither worked. I'm wondering what is the correct syntax. Thank you so much for reading this post.

Upvotes: 0

Views: 128

Answers (5)

Aposhian
Aposhian

Reputation: 869

Your problem is that since random() is a member function of RandomNumber and so it can only be called when you instantiate an instance of that class, using the constructor to make an actual object. If you don't really need a specific instance of that class (which it looks like you don't) you can make the function static:

static int random();

This way it can be called from other classes or main code (as long as Random.h is included) just by using RandomNumber::random(), because static member functions don't depend on specific instances of a class. See http://en.cppreference.com/w/cpp/language/static

Essentially, a static doesn't have the implicit parameter this, a pointer to the instance of the object that a regular member function is called on:

x.useSomeMethod(); // then inside the call of useSomeMethod(), *this == x

However, for your application, the RandomNumber class doesn't seem like it needs to be its own class at all. What you can do if you want to use it in PowerballTicket, but not make it available to the code that uses that class, is to make random() a private member function of PowerballTicket, and this can also be static since (I'm assuming) it doesn't care what instance of PowerballTicket it is being used inside:

class PowerballTicket{
public:
    PowerballTicket(int x1, int x2, int x3, int x4, int x5, int x6);
    int getBall1();
    int getBall2();
    int getBall3();
    int getBall4();
    int getBall5();
    int getPowerball();
private:
    static int random(int min, int max, bool minInclusive = true, bool maxInclusive = true);
    int mBall1;
    int mBall2;
    int mBall3;
    int mBall4;
    int mBall5;
    int mPowerball;
};

But I'm not sure where you are actually planning on calling random() from the code you gave, so you may want to modify this as needed.

Upvotes: 0

Ethan Fischer
Ethan Fischer

Reputation: 1489

Also, don't forget to add

#include "RandomNumber.h"

to the top of your PowerBall.cpp. Otherwise PowerBall won't know what RandomNumber is!

Upvotes: 0

James Adkison
James Adkison

Reputation: 9602

The function signature of the random function is:

int RandomNumber::random();

This returns an int and does not take any parameters. Therefore calling random(1, 69) is wrong.

Furthermore, the random function isn't static. Therefore calling RandomNumber::random(1, 69) is wrong (also because it doesn't accept any parameters).

In order to call the random function you need to instantiate an object and then call the function using the object instance. For example:

RandomNumber prng(1, 69);
prng.random();

Upvotes: 1

Weak to Enuma Elish
Weak to Enuma Elish

Reputation: 4637

RandomNumber is the name of a class, not an instance, and random is a non-static member function. To call random as it currently is, you would have to create an instance of RandomNumber.

RandomNumber rn(/*...args...*/);
rn.random();

//or

RandomNumber(/*...args...*/).random();

You could instead make random a static member function (or just not a member at all) that takes the constructor parameters as arguments instead.

Upvotes: 0

Michael Albers
Michael Albers

Reputation: 3779

To access a member function you need to instantiate the class.

RandomNumber randNum(1, 69);
int newRandomNumber = randNum.random();

Upvotes: 2

Related Questions