A qustion about Vectors with constructors.

This is a discussion on A qustion about Vectors with constructors. within the C++ Programming forums, part of the General Programming Boards category; Hi all I am going to create a program a Deck ADT that represents a deck of playing cards. The ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    77

    A qustion about Vectors with constructors.

    Hi all

    I am going to create a program a Deck ADT that represents a deck of playing cards. The Deck ADT will use a vector to store 52 individual Crad objects.

    Here is my program

    Code:
    #ifndef deck_h
    #define deck_h
    #include <vector>
    #include <cstdlib>
    class Deck
    {
    public:
             Deck() {card.reverse(52);} // Default constructor, sets the    
                                                             initial size of 52
      
             void shuffle(); // This is what i don't know to do               
    
    
    private:
          
              vector<int> card; // STL vector of type card.
    }; 
    #endif
    I am going to create a public method to shuffle the deck. However, i feel confuse how i should code in this method.
    Which return type i should use? void or int?
    and can i use RANDOM method from c standard library?
    Pls give me some advise, thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    I would use random_shuffle<>.

    gg

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
     Deck() {card.reverse(52);}
    Are you sure you don't mean reserve

    Onto the shuffle:
    There are a couple methods you can employ, one of which is random_shuffle()

    As far as the return type let me ask you this, if the function did return something, what would you use the return for?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    Be aware that the random_shuffle function makes use of the rand function which of course would require you to first call srand to seed the psuedorandom number generator before you started to use it. Otherwise, the shuffling would end up being the same each time you ran the program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    reserve() doesn't set the size of the vector. It just tells the vector to allocate enough memory to be able to grow to a specific size without having to reshuffle. You probably want resize(), or better yet, in this situation, pass the deck size directly to the vector's constructor in the intialization list.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    I have modified the program like this:
    Code:
    #ifndef deck_h
    #define deck_h
    #include <vector>
    #include <cstdlib>
    class Deck
    {
    public:
    	Deck() { card.reserve(52); } // default constructor, sets the initial size of 52
    
    	void random_shuffle(card.begin(), card.end()); // shuffle cards
    
    	Card operator[](int index) { return card[index]; } // Overload the [] operator so that a programmer
    													   // can access each Card in the Deck using array notation.		
    
    
    private:
    	vector<int> card; // STL vector of type card.
    
    
    
    
    };
    #endif
    This is my header file, if i just want to do the class declaration in the C++ header file only.
    What should i do with the void random_shuffle function?

    is it like that?
    Code:
    void random_shuffle() // Do i need to put any parameter inside the branket?
    Thanks

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You misunderstand how to use random_shuffle.

    You would have something like this:
    Code:
    Class Deck
    {
      public:
        void shuffle();
    };
    
    void Deck::shuffle()
    {
      random_shuffle(card.begin(), card.end());
    }

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Oh, i made a big mistake with that.

    Thanks for advise.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    I have already re-modifed the program like this:

    Code:
    //deck.h
    #ifndef deck_h
    #define deck_h
    #include <vector>
    #include <cstdlib>
    class Deck
    {
    public:
    	Deck() { card.reserve(52); } // default constructor, sets the initial size of 52
    
    	void shuffle(); // method of shuffle cards
    
    	Card operator[](int index) { return card[index]; } // Overload the [] operator so that a programmer
    													   // can access each Card in the Deck using array notation.		
    
    
    private:
    	vector<int> card; // STL vector of type card.
    
    
    
    
    };
    #endif
    Here is deck.cpp file
    Code:
    // deck.cpp
    #include <cstdlib>
    #include <iostream>
    #include "deck.h"
    
    void Deck::shuffle()
    {
      random_shuffle(card.begin(), card.end());
    }
    
    Deck& Card::operator[]( const Deck& playingCard )
    In my header file, I have one overload operator []
    Code:
    Card operator[](int index) { return card[index]; } // Overload the [] operator so that a programmer
    However, Do i need to implement it in my deck.cpp file?
    If yes, can anyone suggest me how to code it?
    Thanks.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Until you learn the ins and outs and little quirks stick to putting just the class definations and function prototypes in your header and then put all your function definations in your cpp.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Would you pls give me an example how to figure out for the overload operator[] problem
    Inside the header file
    I should use:
    Code:
    Card operator[](int index)
    and inside the cpp file, i should use it?
    Code:
    Card operator[](int index) { return card[index]; }

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In header:
    Code:
    Card& operator[](int);
    In source
    Code:
    Card& Deck::operator[](int index)
    {
      if ( index >= 0 && index < 52 )
        return card[index];
      // otherwise do some error handling here
    }

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Oh, that's what i need to code in my program.
    Thanks a lot.

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Hey, if i want to know which header file is required for the method random_shuffle?

    Which homepage provide this imformation?

    Thanks again.

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    However, when i try to compile the cpp file, it has two errors:

    Code:
    Card& Deck::operator[](int index)
    {
      if ( index >= 0 && index < 52 )
        return card[index];
    }
    
    error C2440: 'return' : cannot convert from 'int' to 'class Card &'
            A reference that is not to 'const' cannot be bound to a non-lvalue
    and 1 more i think the reason is i haven't coded the #include <someting> inside the header
    Code:
     random_shuffle(card.begin(), card.end());
    error C2065: 'random_shuffle' : undeclared identifier

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

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