Advertisement
neogz

KLASE - Preklapanje operatora pr1

Nov 25th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Kolekcija{
  5.     int * _niz;
  6.     int _trenutno;
  7.     int _max;
  8. public:
  9.     Kolekcija(int max = 5){
  10.         _trenutno = 0;
  11.         _max = max;
  12.         _niz = new int[_max];
  13.     }
  14.     Kolekcija(Kolekcija & org){
  15.         _trenutno = org._trenutno;
  16.         _max = org._max;
  17.        
  18.         _niz = new int[_max];
  19.         for (int i = 0; i < _trenutno; i++)
  20.             _niz[i] = org._niz[i];
  21.  
  22.     }
  23.     ~Kolekcija(){
  24.         delete[]_niz;
  25.         _niz = nullptr;
  26.     }
  27.     void operator += (int element){
  28.         if (_trenutno == _max){
  29.             _max += 5;
  30.             int * temp = new int  [_max];
  31.             for (int i = 0; i < _trenutno; i++)
  32.                 temp[i] = _niz[i];
  33.         }
  34.         _niz[_trenutno] = element;
  35.         _trenutno++;
  36.     }
  37.     void ispis(){
  38.         cout << "---------------------------------------\n";
  39.         for (int i = 0; i < _trenutno; i++)
  40.         {
  41.             cout << _niz[i] << " | ";
  42.         }
  43.         cout << "\n---------------------------------------\n";
  44.  
  45.     }
  46.     friend ostream & operator << (ostream & COUT, Kolekcija & obj);
  47.     int getTrenutno()const{ return _trenutno; };
  48.     int operator [] (int i){
  49.         if (i >= 0 && i < _trenutno){
  50.             return _niz[i];
  51.         }
  52.     }
  53.     Kolekcija operator = (Kolekcija & drugi){
  54.         if (this != &drugi){
  55.             delete[]_niz;
  56.             _trenutno = drugi._trenutno;
  57.             _max = drugi._max;
  58.  
  59.             _niz = new int[_max];
  60.             for (int i = 0; i < _trenutno; i++)
  61.                 _niz[i] = drugi._niz[i];
  62.         }
  63.         return *this;
  64.     }
  65.     bool operator == (Kolekcija & drugi){
  66.         if (this == &drugi) return true;
  67.         if (_trenutno != drugi._trenutno) return false;
  68.         for (int i = 0; i < _trenutno; i++){
  69.             if (_niz[i] != drugi._niz[i]) return false;
  70.         }
  71.         return true;
  72.     }
  73.     bool operator != (Kolekcija & drugi){
  74.         if (this == &drugi) return false;
  75.         if (_trenutno != drugi._trenutno) return true;
  76.         for (int i = 0; i < _trenutno; i++){
  77.             if (_niz[i] != drugi._niz[i]) return true;
  78.         }
  79.         return false;
  80.     }
  81.  
  82. };
  83. ostream & operator << (ostream & COUT, Kolekcija & obj){
  84.     cout << "---------------------------------------\n";
  85.     for (int i = 0; i < obj._trenutno; i++)
  86.     {
  87.         cout << obj._niz[i] << " | ";
  88.     }
  89.     cout << "\n---------------------------------------\n";
  90.     return COUT;
  91. }
  92. Kolekcija operator + (Kolekcija & a, Kolekcija & b){
  93.     Kolekcija temp;
  94.     for (int i = 0; i < a.getTrenutno(); i++)
  95.         temp += a[i];
  96.     for (int i = 0; i < b.getTrenutno(); i++)
  97.         temp += b[i];
  98.  
  99.     return temp;
  100. }
  101.  
  102. int main(){
  103.     cout << "a" << endl;
  104.     Kolekcija a;
  105.     a += 15;
  106.     a += 33;
  107.     a += 72;
  108.     cout << a;
  109.  
  110.     cout << "b" << endl;
  111.     Kolekcija b;
  112.     b += 1;
  113.     b += 69;
  114.     cout << b;
  115.  
  116.     cout << "c = a + b" << endl;
  117.     Kolekcija c = a + b;
  118.     cout << c;
  119.  
  120.     cout << "c = a" << endl;
  121.     c = a;
  122.     cout << c;
  123.  
  124.  
  125.     cout << "---------------------------" << endl;
  126.     if (a != a) cout << "a != a  TACNO" << endl;
  127.     else cout << "a != a  NETACNO" << endl;
  128.  
  129.     system("pause > null");
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement