Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //intarray.hpp
- //napisati klasu intArray i izvrsiti preklapanje operatora, +,=,+=,[], <<, ==, !=
- #ifndef INTARRAY_DEF
- #define INTARRAY_DEF
- #include <iostream>
- using namespace std;
- class IntArray{
- private:
- int brEl; // n elemenata
- int *el; // pocetak na celobrojni niz el
- public:
- //konstruktori
- IntArray() {brEl=0; el=NULL;} //inicijalizacija
- IntArray(const int[], int); //konstruktor sa parametrima, sadrzi polja klase
- IntArray(const IntArray&); //konstruktor kopije
- ~IntArray() {delete[] el;} //sprecava se "curenje" memorije
- int lenght()const {return brEl;}
- //operatori za preklapanje
- friend bool operator==(const IntArray&, const IntArray&);
- friend bool operator!=(const IntArray&, const IntArray&);
- friend IntArray operator+(const IntArray&, const IntArray&);
- IntArray& operator+=(const IntArray&);
- IntArray& operator=(const IntArray&);
- //operatori indeksiranja
- int& operator[](int); //upisivanje na a[i]
- int operator[](int)const; //citanje sa a[i]
- friend ostream& operator<<(ostream&, const IntArray&);
- };
- #endif
- //intarray.cpp
- #include "intarray.hpp"
- IntArray:: IntArray(const int ulazniNiz[], int n){ //brEl, el
- brEl = n;
- el = new int[brEl];
- for(int i=0; i<brEl; i++)
- el[i] = ulazniNiz[i];
- }
- IntArray:: IntArray(const IntArray &ray){
- // koristi referencu ray! za brEl i el
- brEl = ray.brEl;
- el = new int[brEl];
- for(int i=0; i<brEl; i++)
- el[i] = ray.el[i];
- }
- bool operator==(const IntArray &ray1, const IntArray &ray2){
- if(ray1.brEl!=ray2.brEl) return false;
- for(int i=0; i<ray1.brEl; i++)
- if(ray1.el[i]!=ray2.el[i]) return false;
- return true;
- }
- bool operator!=(const IntArray &ray1, const IntArray &ray2){
- if(ray1.brEl!=ray2.brEl) return true;
- for(int i=0; i<ray1.brEl; i++)
- if(ray1.el[i]!=ray2.el[i]) return true;
- return false;
- }
- IntArray operator+(const IntArray &ray1, const IntArray &ray2){
- //koristi reference!! vraca se klasni tip
- IntArray tmp; //privremeni objekat
- tmp.brEl = ray1.brEl + ray2.brEl;
- tmp.el = new int[tmp.brEl];
- for(int i=0; i<ray1.brEl; i++)
- tmp.el[i] = ray1.el[i];
- for(int j=0; j<ray2.brEl; j++)
- tmp.el[ray1.brEl+j] = ray2.el[j];
- return tmp;
- }
- IntArray& IntArray:: operator+=(const IntArray &ray){
- //koristi referencu! vraca se referenca
- //formiraj privremeni niz
- //kombinacija
- int *tmpArray = new int [brEl + ray.brEl];
- for(int i=0; i<brEl; i++)
- tmpArray[i] = el[i];
- for(int j=0; j<ray.brEl; j++)
- tmpArray[brEl+j] = ray.el[j];
- brEl+=ray.brEl;
- delete []el;
- el = tmpArray;
- return *this;
- }
- IntArray& IntArray::operator=(const IntArray &ray){
- //proveravaju se adrese objekata koji se porede a=b, a=a!!
- if(this!= &ray) {
- delete []el;
- brEl = ray.brEl;
- el = new int [brEl];
- for(int i=0; i<brEl; i++)
- el[i] = ray.el[i];
- }return *this;
- }
- int& IntArray:: operator[](int i){
- return el[i];
- }
- int IntArray::operator[](int i)const{
- return el[i];
- }
- ostream& operator<<(ostream &out, const IntArray &ray){
- int i;
- for(i=0; i<ray.brEl; i++)
- out<< ray.el[i] << " ";
- return out;
- }
- //main.cpp
- #include "intarray.hpp"
- int main(){
- const int a1[] = {1,2,3};
- const int a2[] = {4,5,6};
- const int a3[] = {1,2,3};
- IntArray a(a1,3), b(a2,3), c(a3,3);
- cout<<"a: "<<a<<endl;
- cout<<"b: "<<b<<endl;
- cout<<"c: " <<c<<endl;
- //testiranje +
- cout<<"a+b: "<< (a+b)<<endl;
- //testiranje +=
- cout<<"a+=c: "<< (a+=c)<<endl;
- //testiranje [] citanje i upis
- cout<<"a[7]: "<< a[7] <<endl;
- a[3] = 6100;
- cout<<"a: "<<a<<endl;
- //testiranje ==
- cout<<"a==b?? " <<(a==b)<<endl;
- //testiranje !=
- cout<<"a!=b?? " <<(a!=b)<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement