Advertisement
fcamuso

Corso recupero C++ - video 29

Dec 13th, 2022
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include "Dado.h"
  4.  
  5. using namespace std;
  6.  
  7. class Dado2 {
  8.  
  9. public:
  10.   Dado2() : Dado2(6) { }
  11.  
  12.   Dado2(int _num_facce)
  13.   {
  14.     if (_num_facce>0) {
  15.       num_facce = _num_facce;
  16.  
  17.       if (primo_dado) {
  18.         srand(time(0));
  19.         primo_dado=false;
  20.       }
  21.     }
  22.  
  23.   }
  24.  
  25.   //un getter
  26.   int get_valore()  { return valore; }
  27.  
  28.   //un setter
  29.   bool set_numero_facce(int nuovo_valore) {
  30.     if (nuovo_valore>0 && nuovo_valore<=20)
  31.     {
  32.       valore = nuovo_valore;
  33.       return true;
  34.     }
  35.     else
  36.       return false;
  37.   }
  38.  
  39.   int lancia() {
  40.     valore = rand()%num_facce + 1;
  41.     return valore;
  42.   }
  43.  
  44. private:
  45.   int num_facce = 0;
  46.   int valore = 0;
  47.  
  48.   //per gestire srand
  49.   static bool primo_dado;
  50.  
  51.  
  52. };
  53. bool Dado2::primo_dado = true;
  54.  
  55. int main()
  56. {
  57.  
  58.   Dado2 d = Dado2();
  59.   Dado2 d2 = Dado2(20);
  60.  
  61.   for (int i=0; i<50; i++)
  62.   {
  63.     d = Dado2();
  64.     cout << d.lancia() << endl;
  65.   }
  66.  
  67.   return 0;
  68.  
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement