Guest User

ListaPacchetti

a guest
May 29th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. *******LISTAPACCHETTI.h*******
  2.  
  3. #include <iostream>
  4. #include "Pacchetto.h"
  5. #include "OorExc.h"
  6.  
  7. using namespace std;
  8.  
  9. typedef PacchettoRegalo A;
  10.  
  11. class ListaPacchetti {
  12.             friend ostream & operator<<(ostream&, const ListaPacchetti &);
  13.             friend istream & operator>>(istream&, ListaPacchetti &);
  14.             private:
  15.                static int count;
  16.                A * V;
  17.                int Nelem;
  18.             public:
  19.                explicit ListaPacchetti(const int = 10);
  20.                ListaPacchetti (const int, const A &);
  21.                ListaPacchetti (const ListaPacchetti &);
  22.                int get_Nelem() const {return Nelem;}
  23.                static int get_count() {return count;}
  24.                A & operator[](int);          
  25.                const A & operator[](int) const;      
  26.                ~ListaPacchetti() {count--; delete [] V;}
  27.                };
  28.                
  29. #endif
  30.  
  31. *******LISTAPACCHETTI.cpp*******
  32.  
  33. #include "ListaPacchetti.h"
  34.  
  35. int ListaPacchetti::count=0; //inizializzazione della variabile membro statica
  36.  
  37. ListaPacchetti::ListaPacchetti(const int n) {
  38.   count++;                    
  39.   Nelem=n;
  40.   V=new A[Nelem];                                          
  41. }
  42.  
  43. ListaPacchetti::ListaPacchetti(const int n, const A & e) {
  44.   count++;  
  45.   Nelem=n;
  46.   V=new A[Nelem];  
  47.   for(int i=0; i<Nelem; i++)
  48.       V[i]=e;                                      
  49. }
  50.  
  51. ListaPacchetti::ListaPacchetti(const ListaPacchetti & L) : Nelem(L.Nelem){
  52.   count++;
  53.   V=new A[Nelem];  
  54.   for(int i=0; i<Nelem; i++)
  55.       V[i]=L.V[i];                                      
  56. }
  57.  
  58. A & ListaPacchetti::operator[](int index){
  59. return V[index];
  60. }
  61.  
  62. const A & ListaPacchetti::operator[](int index) const {
  63. return V[index];
  64. }
  65.  
  66. ostream & operator<<(ostream& os, const ListaPacchetti & L)
  67. {
  68.   for(int i=0; i<L.Nelem; i++)
  69.       os << L.V[i] << endl;
  70.   return os;                
  71. }  
  72.  
  73. istream & operator>>(istream& in, ListaPacchetti & L){
  74. cout << "\n Inserire la dimensione della lista di pacchetti: ";
  75. in >> L.Nelem;
  76. if(L.V) delete [] L.V;
  77. L.V=new A[L.Nelem];
  78. cout << "\n Inserire la lista di pacchetti: " << endl;
  79. for(int i=0; i<L.Nelem; i++)
  80.       in >> L.V[i];
  81. return in;                
  82. }
Advertisement
Add Comment
Please, Sign In to add comment