Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #pragma once
  2.  
  3.  
  4. template <class T>
  5. class Vector {
  6.     template <class U> friend class Vector;
  7.  
  8. private:
  9.     int size; //mowi o ilosci elementow w tablicy
  10.     T *tab;
  11.  
  12. public:
  13.     //konstrutor
  14.     Vector(int size){
  15.         tab = new T[size];
  16.         this->size = size;
  17.     };
  18.     //konkstuktor kopiujacy
  19.     Vector(Vector<T> &ref) {
  20.         this->size = ref.size;
  21.         this->tab = new T[ref.size];
  22.  
  23.         for (int i = 0; i < ref.size; i++) {
  24.             this->tab[i] = ref.tab[i];
  25.         }
  26.     };
  27.  
  28.     //destruktor
  29.     ~Vector(){
  30.         delete[]tab;
  31.     };
  32.  
  33.     //operator indeksowania
  34.     T& operator[](int idx){
  35.         return tab[idx];
  36.     };
  37.  
  38.     //operator przypisania
  39.     Vector <T> operator = (Vector<T>&ref) {
  40.  
  41.         for (int i = 0; i < ref.size; i++) {
  42.             this->tab[i] = ref.tab[i];
  43.         }
  44.         return *this;
  45.     };
  46.  
  47.     //operator przypisania, 2, żeby działalo.  Musiy zaprzyjznic nowy class
  48.     template <class U>
  49.     Vector <T> operator = (Vector<U>&ref) {
  50.  
  51.         for (int i = 0; i < ref.size; i++) {
  52.             this->tab[i] = ref.tab[i];
  53.         }
  54.         return *this;
  55.     };
  56.  
  57.     //operator dodawania        T i U, dwa razy musi być, nie mzoe byc przyjazni miedzy int a int
  58.     Vector<T> operator+(Vector<T> &ref) {
  59.         Vector<T> wynik(ref.size);
  60.  
  61.         for (int i = 0; i < ref.size; i++) {
  62.             wynik.tab[i] = this->tab[i] + ref.tab[i];
  63.         }
  64.  
  65.         return wynik;
  66.     };
  67.  
  68.     //operator dodawania szablonowy
  69.     template <class U>
  70.     Vector<T> operator+(Vector<U> &ref) {
  71.         Vector<T> wynik(ref.size);
  72.  
  73.         for (int i = 0; i < ref.size; i++) {
  74.             wynik.tab[i] = this->tab[i] + ref.tab[i];
  75.         }
  76.             return wynik;
  77.     };
  78.  
  79. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement