Advertisement
Guest User

Untitled

a guest
Jan 15th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. template<class T, unsigned int size> class C;
  2.  
  3. template<class T, unsigned int size> std::ostream& operator<<(std::ostream&, const C<T, size>& c);
  4.  
  5. template<class T, unsigned int size>
  6. class C
  7. {
  8.     template<class T1> class MultiInfo;
  9.    
  10.     friend class MultiInfo<T>; //MultiInfo รจ amica solo quando T1=T
  11.  
  12.     friend std::ostream& operator<< <T,size> (std::ostream&, const C<T, size>& c);
  13.  
  14. private:
  15.     //1)
  16.     template<class T1>
  17.     class MultiInfo {
  18.     public:
  19.         T1 informazione;
  20.         int molteplicita;
  21.         MultiInfo() {}
  22.         MultiInfo(T1 info, int m) : informazione(info), molteplicita(m) {}
  23.         bool operator==(const MultiInfo& m) const {
  24.             if (this == &m)
  25.                 return true;
  26.             else
  27.                 return (informazione == m.informazione && molteplicita == m.molteplicita);
  28.         }
  29.         bool operator!=(const MultiInfo& m) const {
  30.             if (this == &m)
  31.                 return false;
  32.             else
  33.                 return (informazione != m.informazione && molteplicita != m.molteplicita);
  34.         }
  35.     };
  36.  
  37.     MultiInfo<T>* a; //2) Array di oggetti multiinfo
  38.  
  39. public:
  40.    
  41.     //3)
  42.     C(const T& t, int k) : a(new MultiInfo<T>[size]) {
  43.         for (unsigned int i = 0; i < size; i++) {
  44.             a[i] = MultiInfo<T>(t, k >= 1 ? k : 0);
  45.         }
  46.     }
  47.  
  48.     //4)
  49.     C(const C& c) : a(new MultiInfo<T>[size]) {
  50.         for (unsigned int i = 0; i < size; i++) {
  51.             a[i] = c.a[i];
  52.         }
  53.     }
  54.  
  55.     C& operator=(const C& c) { //costruttore di copia profonda
  56.        
  57.         if (this != &c) {
  58.             delete[] a;
  59.             a = new MultiInfo<T>[size];
  60.             for (unsigned int i = 0; i < size; i++)
  61.                 a[i] = c.a[i];
  62.             }
  63.  
  64.         return *this;
  65.     }
  66.  
  67.     ~C() {
  68.         delete[] a;
  69.     }
  70.  
  71.     //5)
  72.     T* operator[](int k) const {
  73.         if (k >= 0 && k < size)
  74.             return &a[k].informazione;
  75.         else
  76.             return nullptr;
  77.  
  78.     }
  79.  
  80.     //6)
  81.     int occorrenze(const T& t) const {
  82.         int k = 0;
  83.         for (unsigned int i = 0; i < size; i++)
  84.             if (a[i].informazione == t)
  85.                 k += a[i].molteplicita;
  86.  
  87.         return k;
  88.     }
  89.  
  90.     //8)
  91.     bool operator==(const C& c) const {
  92.         if (this == &c)
  93.             return true;
  94.         else
  95.             for (unsigned int i = 0; i < size; i++)
  96.                 if (a[i] != c.a[i])
  97.                     return false;
  98.         return true;
  99.     }
  100.  
  101. };
  102.  
  103. //7)
  104. template<class T, unsigned int size> std::ostream& operator<<(std::ostream& os, const C<T, size>& c) {
  105.     for (unsigned int i = 0; i < size; i++)
  106.         os << "Informazione: " << c.a[i].informazione << " Molteplicita': " << c.a[i].molteplicita << std::endl;
  107.     return os;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement