Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- using namespace std;
- template <class T>
- class MyVector
- {
- T* ptr;
- int size;
- static int count; //колво векторов
- friend ostream& operator<<<>(ostream& os, MyVector<T>& obj);
- friend istream& operator>><>(istream& is, MyVector<T>& obj);
- public:
- MyVector(); //0
- MyVector(int size);
- MyVector(const MyVector& obj);
- ~MyVector();
- T& operator=(const T& obj);
- T& operator[](int index);
- static int getCount();
- };
- template <class T>
- int MyVector<T>::count = 0;
- template <class T>
- ostream& operator<<(ostream& os, MyVector<T>& obj);
- template <class T>
- istream& operator>>(istream& is, MyVector<T>& obj);
- template<class T>
- inline MyVector<T>::MyVector()
- {
- size = 0;
- ptr = nullptr;
- count++;
- }
- template<class T>
- inline MyVector<T>::MyVector(int size)
- {
- this->size = size;
- ptr = new T[size];
- count++;
- }
- template<class T>
- inline MyVector<T>::MyVector(const MyVector& obj)
- {
- size = obj.size;
- ptr = new T[size];
- for (int i = 0; i < size; i++) ptr[i] = obj.ptr[i];
- count++;
- }
- template<class T>
- inline MyVector<T>::~MyVector()
- {
- delete[] ptr;
- }
- template<class T>
- inline T& MyVector<T>::operator=(const T& obj)
- {
- if (this == &obj) return *this;
- if (ptr != nullptr) delete[] ptr;
- size = obj.size;
- ptr = new T[size];
- for (int i = 0; i < size; i++) ptr[i] = obj.ptr[i];
- return *this;
- }
- template<class T>
- inline T& MyVector<T>::operator[](int index)
- {
- return ptr[index];
- }
- template<class T>
- inline int MyVector<T>::getCount()
- {
- return count;
- }
- template<class T>
- inline ostream& operator<<(ostream& os, MyVector<T>& obj)
- {
- for (int i = 0; i < obj.size; i++)
- {
- os << obj.ptr[i] << ", ";
- }
- os << "\b\b ";
- return os;
- }
- template<class T>
- inline istream& operator>>(istream& is, MyVector<T>& obj)
- {
- for (int i = 0; i < obj.size; i++)
- {
- cout << i + 1 << " element: ";
- is >> obj.ptr[i];
- }
- return is;
- }
Add Comment
Please, Sign In to add comment