Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int gID = 1;
- int gRef = 20;
- class Datum {
- int d, m, g;
- public:
- Datum(int dan = 0, int mjesec = 0, int godina = 0){
- d = dan;
- m = mjesec;
- g = godina;
- }
- };
- class Kolekcija{
- int * _niz;
- int _trenutno;
- int _max;
- // specijalni slucajevi
- const int _ID;
- int & _referenca;
- Datum _datumKreiranja;
- public:
- Kolekcija() :_ID(0), _referenca(gRef), _datumKreiranja() {
- _trenutno = 0;
- _max = 5;
- _niz = new int[_max];
- }
- Kolekcija(int vrijednost) :_ID(gID), _referenca(gRef), _datumKreiranja(10, 4, 1995){
- _trenutno = 0;
- _max = 5;
- _niz = new int[_max];
- _niz[_trenutno] = vrijednost;
- _trenutno++;
- }
- Kolekcija(Kolekcija & org) :_ID(org._ID), _referenca(org._referenca), _datumKreiranja(org._datumKreiranja){
- _trenutno = org._trenutno;
- _max = org._max;
- _niz = new int[_max];
- for (int i = 0; i < _max; i++)
- _niz[i] = org._niz[i];
- }
- ~Kolekcija(){
- delete[]_niz;
- _niz = nullptr;
- }
- void ispis(){
- if (_trenutno != 0){
- for (int i = 0; i < _trenutno; i++){
- cout << _niz[i] << "\t";
- }
- cout << endl;
- }
- else cout << "Prazna kolekcija.\n";
- }
- void dodajElement(int element){
- if (_trenutno == _max){
- _max += 5;
- int * temp = new int[_max];
- for (int i = 0; i < _trenutno; i++)
- temp[i] = _niz[i];
- delete[]_niz;
- _niz = temp;
- }
- _niz[_trenutno] = element;
- _trenutno++;
- }
- int getTrenutno()const{
- return _trenutno;
- }
- // preklapanje opratora
- void operator += (int element){
- if (_trenutno == _max){
- _max += 5;
- int * temp = new int[_max];
- for (int i = 0; i < _trenutno; i++)
- temp[i] = _niz[i];
- delete[]_niz;
- _niz = temp;
- }
- _niz[_trenutno] = element;
- _trenutno++;
- }
- friend ostream & operator << (ostream & COUT, Kolekcija & obj);
- int operator [] (int lok){
- if (lok >= 0 && lok < _trenutno){
- return _niz[lok];
- }
- }
- Kolekcija operator = (Kolekcija & drugi){
- if (this != &drugi){
- delete[]_niz;
- _trenutno = drugi._trenutno;
- _max = drugi._max;
- _niz = new int[_max];
- for (int i = 0; i < _trenutno; i++){
- _niz[i] = drugi._niz[i];
- }
- }
- return *this;
- }
- bool operator == (Kolekcija & dva){
- if (_trenutno != dva._trenutno) return false;
- for (int i = 0; i < _trenutno; i++){
- if (_niz[i] != dva._niz[i]) return false;
- }
- return true;
- }
- bool operator != (Kolekcija & dva){
- return!(*this == dva);
- }
- };
- ostream & operator << (ostream & COUT, Kolekcija & obj){
- if (obj._trenutno != 0){
- for (int i = 0; i < obj._trenutno; i++){
- COUT << obj._niz[i] << "\t";
- }
- COUT << endl;
- }
- else COUT << "Prazna kolekcija.\n";
- return COUT;
- }
- Kolekcija operator + (Kolekcija & lijevi, Kolekcija & desni){
- Kolekcija temp;
- for (int i = 0; i < lijevi.getTrenutno(); i++)
- temp += lijevi[i];
- for (int j = 0; j < desni.getTrenutno(); j++)
- temp += desni[j];
- return temp;
- }
- int main(){
- Kolekcija a(5);
- a += 1;
- a += 6;
- a += 9;
- a += 10;
- Kolekcija b(a);
- Kolekcija c = a + b;
- cout << a;
- cout << b;
- cout << c;
- system("pause > null");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement