Advertisement
neogz

YT | 7| DM | Preklapanje operatora

Jan 6th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int gID = 1;
  5. int gRef = 20;
  6.  
  7. class Datum {
  8.     int d, m, g;
  9. public:
  10.     Datum(int dan = 0, int mjesec = 0, int godina = 0){
  11.         d = dan;
  12.         m = mjesec;
  13.         g = godina;
  14.     }
  15. };
  16.  
  17. class Kolekcija{
  18.     int * _niz;
  19.     int _trenutno;
  20.     int _max;
  21.  
  22.     // specijalni slucajevi
  23.     const int _ID;
  24.     int & _referenca;
  25.     Datum _datumKreiranja;
  26.  
  27. public:
  28.     Kolekcija() :_ID(0), _referenca(gRef), _datumKreiranja() {
  29.         _trenutno = 0;
  30.         _max = 5;
  31.         _niz = new int[_max];
  32.     }
  33.     Kolekcija(int vrijednost) :_ID(gID), _referenca(gRef), _datumKreiranja(10, 4, 1995){
  34.         _trenutno = 0;
  35.         _max = 5;
  36.         _niz = new int[_max];
  37.  
  38.         _niz[_trenutno] = vrijednost;
  39.         _trenutno++;
  40.     }
  41.     Kolekcija(Kolekcija & org) :_ID(org._ID), _referenca(org._referenca), _datumKreiranja(org._datumKreiranja){
  42.         _trenutno = org._trenutno;
  43.         _max = org._max;
  44.         _niz = new int[_max];
  45.  
  46.         for (int i = 0; i < _max; i++)
  47.             _niz[i] = org._niz[i];
  48.     }
  49.     ~Kolekcija(){
  50.         delete[]_niz;
  51.         _niz = nullptr;
  52.     }
  53.     void ispis(){
  54.         if (_trenutno != 0){
  55.             for (int i = 0; i < _trenutno; i++){
  56.                 cout << _niz[i] << "\t";
  57.             }
  58.             cout << endl;
  59.         }
  60.         else cout << "Prazna kolekcija.\n";
  61.     }
  62.     void dodajElement(int element){
  63.         if (_trenutno == _max){
  64.             _max += 5;
  65.             int * temp = new int[_max];
  66.             for (int i = 0; i < _trenutno; i++)
  67.                 temp[i] = _niz[i];
  68.             delete[]_niz;
  69.             _niz = temp;
  70.         }
  71.         _niz[_trenutno] = element;
  72.         _trenutno++;
  73.     }
  74.     int getTrenutno()const{
  75.         return _trenutno;
  76.     }
  77.     // preklapanje opratora
  78.     void operator += (int element){
  79.         if (_trenutno == _max){
  80.             _max += 5;
  81.             int * temp = new int[_max];
  82.             for (int i = 0; i < _trenutno; i++)
  83.                 temp[i] = _niz[i];
  84.             delete[]_niz;
  85.             _niz = temp;
  86.         }
  87.         _niz[_trenutno] = element;
  88.         _trenutno++;
  89.     }
  90.     friend ostream & operator << (ostream & COUT, Kolekcija & obj);
  91.     int operator [] (int lok){
  92.         if (lok >= 0 && lok < _trenutno){
  93.             return _niz[lok];
  94.         }
  95.     }
  96.     Kolekcija operator = (Kolekcija & drugi){
  97.         if (this != &drugi){
  98.             delete[]_niz;
  99.             _trenutno = drugi._trenutno;
  100.             _max = drugi._max;
  101.             _niz = new int[_max];
  102.             for (int i = 0; i < _trenutno; i++){
  103.                 _niz[i] = drugi._niz[i];
  104.             }
  105.         }
  106.         return *this;
  107.     }
  108.     bool operator == (Kolekcija & dva){
  109.         if (_trenutno != dva._trenutno) return false;
  110.         for (int i = 0; i < _trenutno; i++){
  111.             if (_niz[i] != dva._niz[i]) return false;
  112.         }
  113.         return true;
  114.     }
  115.     bool operator != (Kolekcija & dva){
  116.         return!(*this == dva);
  117.     }
  118. };
  119.  
  120. ostream & operator << (ostream & COUT, Kolekcija & obj){
  121.     if (obj._trenutno != 0){
  122.         for (int i = 0; i < obj._trenutno; i++){
  123.             COUT << obj._niz[i] << "\t";
  124.         }
  125.         COUT << endl;
  126.     }
  127.     else COUT << "Prazna kolekcija.\n";
  128.     return COUT;
  129.  
  130. }
  131. Kolekcija operator + (Kolekcija & lijevi, Kolekcija & desni){
  132.     Kolekcija temp;
  133.     for (int i = 0; i < lijevi.getTrenutno(); i++)
  134.         temp += lijevi[i];
  135.  
  136.     for (int j = 0; j < desni.getTrenutno(); j++)
  137.         temp += desni[j];
  138.  
  139.     return temp;
  140.  
  141. }
  142.  
  143. int main(){
  144.  
  145.     Kolekcija a(5);
  146.     a += 1;
  147.     a += 6;
  148.     a += 9;
  149.     a += 10;
  150.  
  151.     Kolekcija b(a);
  152.     Kolekcija c = a + b;
  153.  
  154.     cout << a;
  155.     cout << b;
  156.     cout << c;
  157.  
  158.  
  159.     system("pause > null");
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement