Hello,
I have a problem. I got this headerfile. And am trying to make a class from it and call it from a seperate main.
It is supposed to roll a six sided dice. I know how to do the simple code but I dont understand how the cpp works with the .h file. Can anybody explain/show to me what part should be written in the .cpp for the class to work properly.
This is the header file:
------------------------------------------------
-------------------------------Code:#ifndef DIE_H #define DIE_H #include <time.h> #include <stdlib.h> /** * A pseudo random die with any number of sides **/ class Die { public: /** * Constructs a die with 6 sides */ Die() { mSides = 6; initialize(); } /** * Constructs a die with any number of sides * * @param sides number of sides for the die */ Die(unsigned int sides) { mSides = sides; initialize(); } /** * Cleans up allocated resources */ virtual ~Die() { } /** * Rolls the die and produces a pseudo random number between 1 and number of sides * * @return int die value */ int rollDie() { mValue = rand() % mSides + 1; return mValue; } /** * Gets the current value of the die without rerolling it * * @return int die value */ const int getDieValue() { return mValue; } /** * Operator < that can be used to compare two dice objects. * A comparison of two dice will only consider the dice value * and not the number of sides of the dice. * * <code>Die die1(6); //with value 5<br> * Die die2(20); //with value 4<br> * if(die1 < die2) //will return false</code> * * @param die the other die that we want to compare with * @return bool true if the other dice has a greater value */ bool operator< (const Die& die) const { return mValue < die.mValue; } /** * Operator > that can be used to compare two dice objects. * A comparison of two dice will only consider the dice value * and not the number of sides of the dice. * * <code>Die die1(6); //with value 5<br> * Die die2(20); //with value 4<br> * if(die1 > die2) //will return true</code> * * @param die the other die that we want to compare with * @return bool true if the other dice has a smaller value */ bool operator> (const Die& die) const { return mValue > die.mValue; } /** * Operator == that can be used to compare two dice objects. * A comparison of two dice will only consider the dice value * and not the number of sides of the dice. * * <code>Die die1(6); //with value 5<br> * Die die2(20); //with value 5<br> * if(die1 == die2) //will return true</code> * * @param die the other die that we want to compare with * @return bool true if the other dice has the same value */ bool operator== (const Die& die) const { return mValue == die.mValue; } private: /** * Initiates the die */ void initialize() { srand (mInitiator ++); rollDie(); } static unsigned int mInitiator; int mValue; unsigned int mSides; }; unsigned int Die::mInitiator = static_cast<unsigned int>(time(NULL)); #endif
This is what I thought to write in the .cpp file:
-------------------------------------------------------------------
------------------------------------------------Code:// Class automatically generated by Dev-C++ New Class wizard #include "die.h" // class's header file #include <iostream> using namespace std; // class constructor void Die::rollDie() { int face; face = rollDie(); cout << face << endl; }
So, I guess it is a bit complicated. I am trying to learn from a book but I dont understand this part.
Thank you in advance for any help.
/Vastgoten.



LinkBack URL
About LinkBacks



