質問

My Professor Assigned us this homework, however I cant make sense on how to do the first step. I am not asking anybody to do the homework, just please someone help me with the first question

  1. Create a Deck dynamically

This question is based on two files (which he provieded)

"card.h"

//*********************************************************
//  CLASS DECLARATION
//*********************************************************
class Card
{   
    //*****************************************************
    //  Public Members         
    //*****************************************************
   public: 
    // Exception classes
    class NotInitalized {};

    // Enumeration for Suit
    enum Suit { Clubs, Diamonds, Hearts, Spades, 
               UNKNOWN };

    // Enumeration for Card Name
       enum CardName { Ace, Two, Three, Four, Five, Six,
                    Seven, Eight, Nine, Ten, Jack, 
                    Queen, King, UNKNOWN };

    // constructors
//*****************************************************
// Card
//  
// Create uninitialized card.  Must be initialized with 
//  'setCard()' before use.
//*****************************************************
    Card();    // card is not initialized
//*****************************************************
// Card
//  
// Create a card based its ordinal position.
//     cards are ordered by suit first in the order 
//     Clubs, Diamonds, Hearts and Spades, and within 
//     the suit they are ordered Act thru King.
//******************************************************
    Card(int); // number between 1-52

//******************************************************
// Card
//  
// Create a card with the given name and suit.
//*******************************************************
       Card(Suit, CardName);

    // methods
    //*******************************************************
    // setCard
    //  
    //  Set the Suit and Name of the card
    //*******************************************************
        void setCard(Suit, CardName);

    //*******************************************************
    // getSuit
    //  
    //  returns the element of the Suit enumeration 
    // representing the suit of the card
    //*******************************************************
    int      getSuit();

    //*******************************************************
    // getCardName
    //  
    //  returns the element of the CardName enumeration 
    //  representing the card
    //*******************************************************
    int    getCardName();

    //*******************************************************
    // getCardValue
    //  
    //  returns face value of card.  For Ace -1 is the value.
    //*******************************************************
    int         getCardValue();

     //*****************************************************
     // toString
     // 
     // return the string representation of the card. 
     //    e.g.,"Ace of Spades"
     //*****************************************************
       string      toString();

//************************************************
//  Private Members    
//************************************************
private:
    // the Card’s suit (uses Suit enum)
    Suit      suit = Suit::UNKNOWN; 

    // the Card’s name (uses CardName enum)
    CardName  name = CardName::UNKNOWN; 
};

The second class is the Deck class. This class represents the 52 cards in a standard poker deck. Internally the cards in the deck should be maintained in an array of Card objects. There should also be a parallel array of Card pointers where the order of the cards after each shuffle can be stored.

When a Deck object is created it creates the 52 cards and shuffles them. If the deck runs out of cards before it is reshuffled the dealCard() method should throw a DeckEmpty Exception.

Because this class creates card objects, it should have a destructor that deletes all the related card objects when the Deck is deleted.

Below is the class declaration for the Deck class.

//*********************************************************
//  CLASS DECLARATION
//*********************************************************
#include “Card.h”
class Deck
{
//*****************************************************
// Public Members         
//*****************************************************
public: 
    // Exception classes
    class DeckEmpty {};

    // Constructors/Destructors
    Deck(); // creates the cards and sorts them
   ~Deck(); // frees all the cards

    // Methods
    //****************************************************
    // dealCard
    //
    // return the next available card in the shuffled deck
    //****************************************************
    Card      dealCard();

    //****************************************************
    // shuffle
    //
    // shuffle the cards
    //****************************************************
    Void      shuffle();      // shuffle the deck


    //****************************************************
    // getCardCount
    //
    // return the number of unused cards in the shuffled 
    //     deck
    //****************************************************
       int       getCardCount(); // how many cards left

    //****************************************************
    // toString
    //
    // return a newline (\n) delimited list of the shuffled 
    //    cards 
    //*****************************************************
    string    toString();

//*****************************************************
// Private Members    
//*****************************************************
private:
    // array to hold unshuffled cards
       Card      cards[DECK_SIZE];

    // array to hold shuffled cards
    Card*     shuffledCards[DECK_SIZE];

    // index of next card to deal from shuffled cards
       int       nextCardIndex;    
};
役に立ちましたか?

解決

It really makes no sense to me either. Particularly the bit about parallel array of shuffled cards is unclear. And the statement that it needs a destructor because it creates Card objects is just incorrect. Perhaps he means that it creates Card objects dynamically, but firstly that not what he said, and secondly I don't see the need. I think you should have a talk with your professor.

However the first step is very easy.

'1. Create a Deck dynamically'

Deck *my_deck = new Deck;

Solved.

Why you have to create a deck dynamically is another question, but that's what he asked you do to.

I'm not at all sure your professor knows what he's talking about.

他のヒント

My Professor Assigned us this homework, however I cant make sense on how to do the first step. I am not asking anybody to do the homework, just please someone help me with the first question

  1. Create a Deck dynamically

This is the answer:

Deck* obj = new Deck();

What you can see above is new expression. http://en.cppreference.com/w/cpp/language/new With this expression new objects are created dynamically.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top