我嘗試初始化一個類的數組,然後使用循環更改每個對象中的數據成員。我不知道如何讓這些值保持不變,因爲在我更改了這些值之後,我試圖打印出一個隨機對象,它只是默認值而不是更改後的值。任何幫助將不勝感激,謝謝!在C++中初始化類
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class card {
private:
int rank;
int suit;
////////////////////////////////////////////////////////////
public:
/////////////////////////////////////////////////////////////
// default constructor with initialization list
card(int userRank = (2,3,4,5,6) , int userSuit=15)
:rank(userRank), suit(userSuit){}
///////////////////////////////////////////////////////////
// function to validate user's rank choice.
int cardcheckRank(int pRank){
while(pRank<2 || pRank>14)
{
cout << "Choose a playing card rank between 2-14, where 11=Jack, "
"12=Queen, 13=King, 14=Ace"<<endl;
cin >> pRank;
}
return pRank;
}
/////////////////////////////////////////////////////////////
// function to validate user's suit choice.
int cardcheckSuit(int pSuit){
while(pSuit<15 || pSuit>18)
{
cout << "Choose a playing card suit "
"between 15-18, where 15=Diamond, 16=Club, 17=Heart, 18=Spades.";
cin >> pSuit;
}
return pSuit;
}
//////////////////////////////////////////////////////////////////
// functioin to get a card value from user.
void storeCard(int pRank, int pSuit){
card(cardcheckRank(pRank),cardcheckSuit(pSuit));
}
/////////////////////////////////////////////////////////////////
// translates
string faceRank(int translateRank){
switch (translateRank) {
case 2:
return "Two";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
case 10:
return "Ten";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
case 14:
return "Ace";
default: return "Invalid";
}
}
///////////////////////////////////////////////////////////
// translate integer suit value into a word.
string faceSuit(int translateSuit){
switch (translateSuit){
case 15:
return "Diamonds";
break;
case 16:
return "Clubs";
break;
case 17:
return "Hearts";
break;
case 18:
return "Spades";
break;
default: return "Invalid";
}
}
///////////////////////////////////////////////////////
// Function to print the current card.
void printCard(){
cout << "The rank of the card is ";
cout << faceRank(rank);
cout << " and the suit is " << faceSuit(suit) << "." << endl;
}
}; // End of card class.
//////////////////////////////////////////////////////
// main function.
int main()
{
srand (time(NULL)); // initialize random seed.
card deck[52];
char choice = 'n';
int h = 0; // card number.
for(int i = 15; i < 19; i++)
{
int y = i;
for(int j =2; j < 15; j++)
{
int z = j;
(deck [h]).storeCard(z,y);
cout << "Card rank " << (deck [h]).faceRank(z);
cout << ", suit " << (deck [h]).faceSuit(y) <<endl;
++h;
}
}
do{
cout <<"Would user like to play?(y/n)"<<endl;
cin >> choice;
int ranNum = (rand()% 51 + 0);
if(choice == 'y')
{
cout << "User: ";
deck[7].printCard();
}
}
while(choice == 'y');
return 0;
}
在你的構造函數中發生了一些奇怪的事情。 'userRank'的默認值是6.稍後,你創建一個'card'對象並將其扔掉。 – chris
我真的不明白,但我試圖設置一個默認值,只給對象一些東西,但是打算讓整個數組成爲一副牌,每張牌都是一個對象。 – Davy2468