0

I have a Deck class that instantiates your standard 52-card deck. I'm making a Durak card game, which allows you to use different sized decks. So I'm inheriting Deck, with a new DurakDeck class.

I have the following DurakDeck constructor (I also have a default constructor, that more or less does something similar). I'm running into an issue, where it looks like when I instantiate a DurakDeck, it is also calling the Deck constructor, because my DurakDeck object ends up containing however many cards it was told to instantiate in it's constructor, plus an extra 52-cards (coming from Deck).

Does the parent class constructor call automatically? I always assumed it only called if you had the : base() specified...?

Not really sure where I am going wrong...

public DurakDeck(byte deckSize)
        {
            const Rank SMALL_DECK_START = Rank.Ten;
            const Rank NORMAL_DECK_START = Rank.Six;
            const Rank LARGE_DECK_START = Rank.Deuce;

            this.deckSize = (deckSize - 1);
            Rank startingValue = Rank.Six;

            // Check what deckSize is equal to, and start building the deck at the required card rank.
            if (deckSize == SMALL_SIZE) { startingValue = SMALL_DECK_START; }
            else if (deckSize == NORMAL_SIZE) { startingValue = NORMAL_DECK_START; }
            else if (deckSize == LARGE_SIZE) { startingValue = LARGE_DECK_START; }
            else { startingValue = NORMAL_DECK_START; }

            // Ace is 1 in enum, and Durak deck can initialize at different sizes,
            //  so we add the aces of each 4 suits first.
            for (int suitVal = 0; suitVal < 4; suitVal++)
            {
                cards.Add(new Card((Suit)suitVal, Rank.Ace));
            }

            // Loop through every suit
            for (int suitVal = 0; suitVal < 4; suitVal++)
            {
                // Loop through every rank starting at the starting value determined from the if logic up above.
                for (Rank rankVal = startingValue; rankVal <= Rank.King; rankVal++)
                {
                    // Add card to deck
                    cards.Add(new Card((Suit)suitVal, rankVal));
                } 
            }
        }

3 Answers3

0

If your base class has a default constructor it will always be called, unless you specify an alternative one to call.

Sean
  • 60,939
  • 11
  • 97
  • 136
0

Base constructors are called automatically. Using the : Base () gives you the ability to specify inputs into the base constructor.

see for more info Will the base class constructor be automatically called?

Community
  • 1
  • 1
XenoPuTtSs
  • 1,254
  • 1
  • 11
  • 31
  • So would it be ideal for me to add an empty constructor into the Deck class? –  Mar 27 '15 at 14:58
  • I'm not sure how you envision what Deck is responsible for, but you could create a constructor Deck(int deckSize) for Deck and use DurakDeck(byte size):base(size-1) – XenoPuTtSs Mar 27 '15 at 15:02
0
    class ParentClass
    {
        public ParentClass(){
            Console.WriteLine("parent created");
        }
    }
    class ChildClass : ParentClass
    {
        public ChildClass()
        {
            Console.WriteLine("child created");
        }
    }

ChildClass cc = new ChildClass(); prints "parent created", and then prints "child created".

RSSM
  • 669
  • 7
  • 19