Is there an easier way to instantiate many objects?
Feb 12, 2017 at 10:32pm UTC
My teacher once told me if I am using a lot of repetitive code chances are I'm doing something wrong or inefficiently. What I'm trying to do is create a deck of card but instantiating each card (52) seems very redundant. Am i doing this correctly or is there a better method to do this? Keep in mind I am still very much a beginner. This is what I've done so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <iostream>
#include <vector>
using namespace std;
class CardDeck
{
public :
string suite;
int suiteStrength;
int num;
};
int main()
{
//Holds all Diamond cards
vector<CardDeck> diamondSuite;
//Create all
CardDeck threeDiamond;
diamondSuite.push_back(threeDiamond);
CardDeck fourDiamond;
diamondSuite.push_back(fourDiamond);
CardDeck fiveDiamond;
diamondSuite.push_back(fiveDiamond);
CardDeck sixDiamond;
diamondSuite.push_back(sixDiamond);
CardDeck sevenDiamond;
diamondSuite.push_back(sevenDiamond);
CardDeck eightDiamond;
diamondSuite.push_back(eightDiamond);
CardDeck nineDiamond;
diamondSuite.push_back(nineDiamond);
CardDeck tenDiamond;
diamondSuite.push_back(tenDiamond);
CardDeck jackDiamond;
diamondSuite.push_back(jackDiamond);
CardDeck queenDiamond;
diamondSuite.push_back(queenDiamond);
CardDeck kingDiamond;
diamondSuite.push_back(kingDiamond);
CardDeck aceDiamond;
diamondSuite.push_back(aceDiamond);
CardDeck twoDiamond;
diamondSuite.push_back(twoDiamond);
for (int i = 3; i < 13; i ++)
{
diamondSuite[i].suite = "DIAMOND" ;
diamondSuite[i].suiteStrength = 0;
diamondSuite[i].num = i;
}
cout << "SUITE:" << diamondSuite[3].suite << endl;
cout << "SUITE STR: " << diamondSuite[7].suiteStrength << endl;
cout << "NUM: " << diamondSuite [12].num << endl;
}
Topic archived. No new replies allowed.