Advertisement
nex036ara

intarray

Nov 27th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. //intarray.hpp
  2.  
  3. //napisati klasu intArray i izvrsiti preklapanje operatora, +,=,+=,[], <<, ==, !=
  4. #ifndef INTARRAY_DEF
  5. #define INTARRAY_DEF
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. class IntArray{
  10.  
  11.     private:
  12.         int brEl; // n elemenata
  13.         int *el; // pocetak na celobrojni niz el
  14.     public:
  15.         //konstruktori
  16.         IntArray() {brEl=0; el=NULL;} //inicijalizacija
  17.         IntArray(const int[], int); //konstruktor sa parametrima, sadrzi polja klase
  18.         IntArray(const IntArray&); //konstruktor kopije
  19.         ~IntArray() {delete[] el;} //sprecava se "curenje" memorije
  20.         int lenght()const {return brEl;}
  21.         //operatori za preklapanje
  22.         friend bool operator==(const IntArray&, const IntArray&);
  23.         friend bool operator!=(const IntArray&, const IntArray&);
  24.         friend IntArray operator+(const IntArray&, const IntArray&);
  25.         IntArray& operator+=(const IntArray&);
  26.         IntArray& operator=(const IntArray&);
  27.         //operatori indeksiranja
  28.         int& operator[](int); //upisivanje na a[i]
  29.         int operator[](int)const; //citanje sa a[i]
  30.         friend ostream& operator<<(ostream&, const IntArray&);
  31. };
  32. #endif
  33.  
  34. //intarray.cpp
  35.  
  36.  
  37. #include "intarray.hpp"
  38.  
  39.  
  40.  
  41. IntArray:: IntArray(const int ulazniNiz[], int n){ //brEl,  el
  42.     brEl = n;
  43.     el = new int[brEl];
  44.         for(int i=0; i<brEl; i++)
  45.             el[i] = ulazniNiz[i];
  46. }
  47.  
  48.  
  49. IntArray:: IntArray(const IntArray &ray){
  50.     // koristi referencu ray! za brEl i el
  51.     brEl = ray.brEl;
  52.     el = new int[brEl];
  53.         for(int i=0; i<brEl; i++)
  54.             el[i] = ray.el[i];
  55. }
  56.  
  57. bool operator==(const IntArray &ray1, const IntArray &ray2){
  58.     if(ray1.brEl!=ray2.brEl) return false;
  59.         for(int i=0; i<ray1.brEl; i++)
  60.         if(ray1.el[i]!=ray2.el[i]) return false;
  61.             return true;
  62. }
  63. bool operator!=(const IntArray &ray1, const IntArray &ray2){
  64.         if(ray1.brEl!=ray2.brEl) return true;
  65.         for(int i=0; i<ray1.brEl; i++)
  66.         if(ray1.el[i]!=ray2.el[i]) return true;
  67.             return false;
  68. }
  69.  
  70.  
  71. IntArray operator+(const IntArray &ray1, const IntArray &ray2){
  72.         //koristi reference!! vraca se klasni tip
  73.  
  74.         IntArray tmp; //privremeni objekat
  75.         tmp.brEl = ray1.brEl + ray2.brEl;
  76.         tmp.el = new int[tmp.brEl];
  77.             for(int i=0; i<ray1.brEl; i++)
  78.                 tmp.el[i] = ray1.el[i];
  79.             for(int j=0; j<ray2.brEl; j++)
  80.                 tmp.el[ray1.brEl+j] = ray2.el[j];
  81.                 return tmp;
  82. }
  83.  
  84. IntArray& IntArray:: operator+=(const IntArray &ray){
  85.     //koristi referencu! vraca se referenca
  86.     //formiraj privremeni niz
  87.     //kombinacija
  88.     int *tmpArray = new int [brEl + ray.brEl];
  89.     for(int i=0; i<brEl; i++)
  90.         tmpArray[i] = el[i];
  91.     for(int j=0; j<ray.brEl; j++)
  92.         tmpArray[brEl+j] = ray.el[j];
  93.  
  94.         brEl+=ray.brEl;
  95.         delete []el;
  96.         el = tmpArray;
  97.         return *this;
  98.         }
  99.  
  100.  
  101. IntArray& IntArray::operator=(const IntArray &ray){
  102.     //proveravaju se adrese objekata koji se porede a=b, a=a!!
  103.         if(this!= &ray) {
  104.                 delete []el;
  105.                 brEl = ray.brEl;
  106.                 el = new int [brEl];
  107.  
  108.                 for(int i=0; i<brEl; i++)
  109.                 el[i] = ray.el[i];
  110.  
  111.         }return *this;
  112. }
  113.  
  114.         int& IntArray:: operator[](int i){
  115.             return el[i];
  116.         }
  117.         int IntArray::operator[](int i)const{
  118.         return el[i];
  119.         }
  120.         ostream& operator<<(ostream &out, const IntArray &ray){
  121.             int i;
  122.             for(i=0; i<ray.brEl; i++)
  123.             out<< ray.el[i] << " ";
  124.             return out;
  125.         }
  126. //main.cpp
  127.  
  128.  
  129. #include "intarray.hpp"
  130.  
  131. int main(){
  132. const int a1[] = {1,2,3};
  133. const int a2[] = {4,5,6};
  134. const int a3[] = {1,2,3};
  135. IntArray a(a1,3), b(a2,3), c(a3,3);
  136. cout<<"a: "<<a<<endl;
  137. cout<<"b: "<<b<<endl;
  138. cout<<"c: " <<c<<endl;
  139.  
  140.  
  141. //testiranje +
  142. cout<<"a+b: "<< (a+b)<<endl;
  143. //testiranje +=
  144. cout<<"a+=c: "<< (a+=c)<<endl;
  145. //testiranje [] citanje i upis
  146. cout<<"a[7]: "<< a[7] <<endl;
  147. a[3] = 6100;
  148. cout<<"a: "<<a<<endl;
  149. //testiranje ==
  150. cout<<"a==b?? " <<(a==b)<<endl;
  151. //testiranje !=
  152. cout<<"a!=b?? " <<(a!=b)<<endl;
  153.  
  154.  
  155. return 0;
  156. }
  157.  
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement