[C++] OO扑克牌

#include #include using namespace std; enum Suit { Spade = 0, Heart, Diamond, Club }; class Poker { private: Suit m_suit; int m_number; public: Poker(Suit suit,int number) { m_suit = suit; m_number = number; } string GetSuit() { string suit = "Spade"; if(m_suit == Heart) suit = "Heart"; if(m_suit == Diamond) suit = "Diamond"; if(m_suit == Club) suit = "Club"; return suit; } friend ostream& operator <<(ostream& os, Poker* pk) { string suit = pk->GetSuit(); os << "[" << suit.c_str() << ":" << pk->m_number << "]"; return os; } }; class Cards { private: vector m_cards; public: Cards() { for(int i = 0 ; i < 52 ; i++) { m_cards.push_back(new Poker((Suit)(i / 13), (i % 13)+1)); } } void Output() { vector::iterator iter = m_cards.begin(); for(; iter != m_cards.end() ; iter++) { cout<<(Poker*)(*iter)<Output(); int a = 0; a = cin.get(); return 0; }

 

刚刚在看论坛的时候,看到有人问怎么样把发牌做的更OO一点

就做了个简单的OO扑克牌...

利用Poker物件代表每一张牌,有花色跟数字两个属性

然后用Cards来包起来

你可能感兴趣的:(C/C++)