Newsgroup: it.comp.lang.c++
Titolo: Classi in c++

Qualcuno sa dirmi dove posso trovare esempi di classi scritte in c++!


Eccoti un esempio fresco fresco fatto in casa!:
// file dreck.h
#ifndef _FILEOUT_
#define _FILEOUT_
#include <iostream>
#include <vector>
#define OK   0
#define FAIL 1
enum FamilyNapoletane {BASTONI, COPPE, DENARI, SPADE};
// enum FamilyPoker {CUORI, QUADRI, FIORI, PICCHE};
class CGameCard
{
public:
   CGameCard() : mVal(1), mFam(0) {}
   CGameCard(int fam, int val) : mVal(val), mFam(fam) {}
   void Show() const;
   void Set(int fam, int val) {mFam = fam; mVal = val;}
   void Get (int &fam, int &val) const {fam = mFam; val = mVal;}
private:
   int mVal;
   int mFam;
};
typedef std::vector<CGameCard> VecCards;
class CDeck
{
protected :
   int mNumCards;
   int mNumFams;
   int mCurPos;
public:
   CDeck(int numCards, int numFams);
   void ShuffleDeck();
   int GetNumCards() const {return mNumCards;}
   int GiveMeACard (CGameCard &card);
   virtual void Show() const = 0;
   virtual ~CDeck() {std::cout << "Deck destruction" << std::endl;}
   VecCards shuffledDeck;
};
class CDeckNapoletano: public CDeck
{
public:
   CDeckNapoletano();
   void Show() const;
   ~CDeckNapoletano() {std::cout << "Deck napoletano destruction" <<
std::endl;}
private:
   enum {NCARDS = 40, NFAMS = 4};
};
#endif
// file dreck.cpp
#include <stdlib.h>
#include <time.h>
#include "fileout.h"
void CGameCard::Show() const
{
   char cFam;
   switch (mFam)
   {
      case BASTONI: cFam = 'B'; break;
      case COPPE  : cFam = 'C'; break;
      case DENARI : cFam = 'D'; break;
      case SPADE  : cFam = 'S'; break;
   }
   std::cout.width(2);
   std::cout << mVal << cFam << " ";
}
CDeck::CDeck(int numCards, int numFams)  : mNumCards(numCards),
mNumFams(numFams)
{
   mCurPos = 0;
   std::cout << "Deck building" << numCards << " Cards " << numFams << "
Familys " << std::endl;
}
void CDeck::ShuffleDeck()
{
   std::cout << "Deck Shuffling" << std::endl;
   srand( (unsigned)time( NULL ) );
   int randcard;
   for (int ncard = 0; ncard < mNumCards; ncard++)
   {
      randcard = rand()%mNumCards;
      std::swap(shuffledDeck[ncard], shuffledDeck[randcard]);
   }
}
int CDeck::GiveMeACard(CGameCard &card)
{
   std::cout << "->";
   if (mCurPos < mNumCards)
   {
      card = shuffledDeck[mCurPos++];
      return OK;
   }
   else
   {
      std::cout << " No more cards! " << std::endl;
      return FAIL;
   }
}
CDeckNapoletano::CDeckNapoletano() : CDeck(NCARDS, NFAMS)
{
   std::cout << "Deck napoletano building" << std::endl;
   // init the Deck ordering it by values and familys
   int vCard;
   int vFam;
   for (vFam = BASTONI; vFam <= SPADE; vFam++)
      for (vCard = 1; vCard <= (mNumCards/mNumFams); vCard++)
      {
         CGameCard card(vFam, vCard);
         shuffledDeck.push_back(card);
      }
}
void CDeckNapoletano::Show() const
{
   std::cout << std::endl << "Show Deck napoletano" << std::endl;
   // Visit the Deck from the init position card to the last
   VecCards::const_iterator itCurCard = shuffledDeck.begin();
   itCurCard += mCurPos;
   for (int i = 1; itCurCard != shuffledDeck.end(); itCurCard++, i++)
   {
      itCurCard->Show();
      if ((i % (mNumCards/mNumFams)) == 0)
         std::cout << std::endl;
   }
   std::cout << std::endl;
}
int main()
{
// I add a block to see destructors too
   {
      CDeckNapoletano mn;
      mn.ShuffleDeck();
      std::cout << "Cards shuffled:" << std::endl;
      mn.Show();
      CGameCard card;
      int numCard = mn.GetNumCards();
      for (int i = 1; i <= numCard + 1; i++)
      if (OK == mn.GiveMeACard(card))
      {
         card.Show();
         if ((i%10) == 0)
            std::cout << std::endl;
      }
      std::cout << std::endl;
      mn.Show();
   }
   system("PAUSE");
   return 0;
}
// end files
Era un esempio che avevo modificato di un programma letto su questo
newsgroup.
Se vuoi esempi piu' "seri" devi andare su
www.codeguru.com
www.codeproject.com
www.programmazione.it
http://sourceforge.net/
http://www.cplusplus.com/
http://www.planet-source-code.com/
Maurus

 #include "fileout.h"
 ^^^^^^^^^^^^^^^
Ooops!
qui ovviamente era
#include "dreck.h"
Maurus

    srand( (unsigned)time( NULL ) );
Il cast a unsigned serve?
--
Gerolamo

 Il cast a unsigned serve?
Questo l'ho copiato da MSDN, time restituisce un long e srand vuole un
unsigned ma in effetti non e' necessario il cast.
Grazie per le precisazioni.
Maurus