Advertisement
neogz

YT | 5| DM | Konstruktor kopije

Jan 5th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 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.  
  54.  
  55.     void ispis(){
  56.         if (_trenutno != 0){
  57.             for (int i = 0; i < _trenutno; i++){
  58.                 cout << _niz[i] << "\t";
  59.             }
  60.             cout << endl;
  61.         }
  62.         else cout << "Prazna kolekcija.\n";
  63.     }
  64.     void dodajElement(int element){
  65.         if (_trenutno == _max){
  66.             _max += 5;
  67.             int * temp = new int[_max];
  68.             for (int i = 0; i < _trenutno; i++)
  69.                 temp[i] = _niz[i];
  70.             delete[]_niz;
  71.             _niz = temp;
  72.         }
  73.         _niz[_trenutno] = element;
  74.         _trenutno++;
  75.     }
  76. };
  77.  
  78. int main(){
  79.  
  80.     Kolekcija a(5);
  81.     a.ispis();
  82.     a.dodajElement(6);
  83.     a.dodajElement(9);
  84.     a.ispis();
  85.  
  86.     Kolekcija b(a);
  87.     b.ispis();
  88.    
  89.     system("pause > null");
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement