Newbie class question

This is a discussion on Newbie class question within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a problem. I got this headerfile. And am trying to make a class from it and call ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    Newbie class question

    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.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    A class is a blueprint for an object, so you instantiate (create an instance of the class from the class blueprint) like so:

    Die mydie;

    Then you can roll the die by calling its member function, like so:

    mydie.rollDie();

    Perhaps you had better study classes a little more?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You have already implemented all the methods of Die in the header, so there is nothing left to put into the "die.cpp" file (except perhaps the definition of unsigned Die::mInitiator?).

    You would just create Die instance(s) in main() (or any other function you like) and use it like Elysia has shown.

    By the way, it seems that your effort to reseed the random number generator for each Die object is rather useless. rand() relies on global state, so seeding it in one instance of Die resets the seed for all other instances of Die. You might just call srand in main() at the beginning of the code, remove the initialize function (and the static variable) and simply call rollDie in the constructor of Die instead.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Thank you both!

    What part of the headerfile should be in the .cpp to make it work properly. I know how to call the class from main directly. But I want it to be a proper class with full .h and .cpp.

    And, yes I will study classes more. This is part of it, I am trying to get an example of how it works with a little more complicated headerfile.

    Again, thanks!
    Last edited by vastgoten; 07-31-2008 at 12:24 AM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    unsigned int Die::mInitiator = static_cast<unsigned int>(time(NULL));
    That line should be in the cpp file. After that, you can move all the function definitions to the cpp file if you'd like. Since they are all small it is ok to leave them in the header, but to practice "proper" separation you can move them over. For example, to move the getDieValue() function you would change the header to be this:
    Code:
    const int getDieValue() const;
    and add this to the cpp file:
    Code:
    /**
     * Gets the current value of the die without rerolling it
     *
     * @return int die value
     */
    const int Die::getDieValue() const
    {
    	return mValue;
    }
    Note that the part in red is different in the cpp file than in the header, you have to tell the compiler the function is a member of that class since it isn't being defined inside the class anymore. The dark blue consts I added to make the code const correct. It has nothing to do with separating header and source file.

    Just make the same changes (not including the extra const) to all the functions one at a time and try building your program after each one to make sure you did it right.
    Last edited by Daved; 07-31-2008 at 01:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 02:01 PM
  3. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  4. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 09:45 AM
  5. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 05:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21