I have been racking my brain for a bit on this and just can't figure it out.

The book "Object-Oriented Programming C++" by Joyce Farrell pg382 exercise 13.

They're wanting me to make a class named "PlayingCard" that contains 4 fields which are values and suits in both numbered and string format and each is assigned these values on construction. So they're wanting a constructor that puts all of the cards in a fictional deck that is accessed through the PlayingCard?
They're also wanting the "Hand" Class to use a PlayingCard object that would be 5 different cards.
I am confused about how to make the connection/transistion into classes.

Here is what I have so far:

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

const int SIZE = 5;

class PlayingCard
{
    private:
        int faceVal;
        int suitVal;
    public:
        PlayingCard();
};
PlayingCard::PlayingCard()
{
    string cardVal[] = {"","Ace","One","Two","Three","Four","Five","Six","Seven",
        "Eight","Nine","Ten","Jack","Queen","King"};

    string cardSuit[] = {"","Spade","Heart","Clubs","Diamonds"};

}
class Hand
{
    public:
        Hand();
        int getCard(int);
};
Hand::Hand()
{
    PlayingCard aHand[SIZE];

    srand((unsigned)time(NULL));

}
int Hand::getCard(int card)
{
    return card;
}
int main()
{
    Hand playerHand[SIZE];

    for(int x = 0; x < SIZE; ++x)
    {
    }
    system("pause");
    return 0;
};

Recommended Answers

All 4 Replies

Anyone have any suggestions?

First off, your question is quite confusing. I can't gather what it is what you're trying to do, let me try with a different example, see if it makes sense to what I think they are wanting you to do.

Let's assume that you have a Person, each person therefore, has a: Name, age, nationality.. Defined below:

class Person
{
    public:

        Person()
        {
            // default
        }

        Person(std::string name, int age, std::string nation)
        {
            this->theName = name;
            this->theNation = nation;
            this->age = age;
        }

    protected:

        std::string theName; 
        std::string theNation;
        int age;
};

So, far we have created the base class. We now introduce inheritence, let's say that you now have a "Student", well a "Student" shares all the characteristics as a person and therefore you can inherit from this:

class Student : public Person {

    public:

       Student() { } // default 

       Student(std::string theName, int theAge, std::string nation, int num)
          : Person(theName, theAge, nation)
       {
          // now we can do this:
          this->theNum = num;

       }

    protected:

       int theNum;

}

Therefore, when we are creating objects, we no longer have to look at the "Person" class.. We can just do the following:

Student s("Phorce", 10, "British", "00001");

If we had a method in the "Person" called getName we could do the following:

std::string returnName()
{
    return this->theName;
}

We can access it by std::cout << s.returnName() note how we did not define this in the Student class?

This can relate to your problem, you can try and figure it out. I'm not too sure what your problem exactly is, but I hope that this helps slightly.

peace

#include <iostream>
#include <Windows.h>
#include <ctime>
#include <string>
using namespace std;
 class PlayingCard
{
    public: 
        int iFaceValue, iSuit;
        string sFaceValue, sSuit;
        PlayingCard(int, int);

};
 PlayingCard::PlayingCard(int FV, int Suit)
{
    iFaceValue = FV;
    iSuit = Suit;
    if (FV == 1)
    {
        sFaceValue = "A";
    }
        if (FV > 1 && FV < 11)
        {
            sFaceValue = FV.ToString();
        }
            if (FV == 11)
            {
                sFaceValue = "J";
            }
                if (FV == 12)
                {
                    sFaceValue = "Q";
                }
                else
                    {
                        sFaceValue = "K";
                    }
    if (Suit == 1)
        {
            sSuit = "Heart";
        }
        if (Suit == 2)
        {
             sSuit = "Spade";
        }
            if (Suit == 3)
            {
                sSuit = "Diamond";
            }
            else
            {
                sSuit = "Club";
            }
 }
class Hand
{
    public:
        PlayingCard pc;
        bool duplicate;
        Hand();

};
Hand::Hand()
{
    int card[5];
    for(int x=0; x < 5; x++)
    {
        card[x] = rand() % 52 + 1;
    }
    while(duplicate)
    {
        int counter = 0;
        for(int x = 0; x < 5; x++)
        {
            for(int y=0; y<5; y++)
            {
                if(card[x] == card[y] && x != y)
                {
                    counter++;
                    card[y] = rand() % 52 + 1;
                }
            }
        }
        if (counter == 0)
        {
            duplicate = false;
        }
        for (int x = 0; x < 5; x++)
        {
            pc[x] = new PlayingCard((int) card[x] / 13 + 1, card[x] % 13 + 1);
        }
    }
}
int main()
{
    Hand pokerHand;
    pokerHand = new Hand();
    for (int x = 0; x < 5; x++)
        {
            cout << pokerHand.pc[x].sFaceValue << pokerHand.pc[x].sSuit << " ";
        }
    system("pause");
    return 0;
}

Changed some things and now it won't compile. Hopefully this at least shows what im trying to do.

"Create a class named PlayingCard. It contains four fields--a value and suit in both numeric and string format (for example, 12 might be "Queen") for a Standard playing card. Each PlayingCard is assigned its values upon construction. Create another class named Hand which represents a standard poker hand which consists of five PlayingCards. The Hand constructor should randomly assign a value to each of its five PlayingCards, with no duplicates. In other words, there can be multiple 4's and multiple hearts in a Hand, but only one four of hearts. Write a main() function that declares a Hand and displays its values."

This is the actual question from the book if that makes more sense than what I was trying to convey. I am not sure how to edit my previous post or I would.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.