mashlukashova

Untitled

May 27th, 2019
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. using namespace std;
  5.  
  6.  
  7. template<class T>
  8. class Vector {
  9. private:
  10. int size;
  11. T *data;
  12. public:
  13.  
  14. Vector(){
  15. size = 3;
  16. data = new T[size];
  17. data[0] = 0;
  18. }
  19.  
  20.  
  21.  
  22. Vector(int _size){
  23. this->size = _size;
  24. this->data = new T[size];
  25. cout << "Viziv konstr: vvedite " << size << " el vectora: " << endl;
  26. for (int i = 0; i < size; i++){
  27. cin >> data[i];
  28. }
  29. }
  30.  
  31. Vector(Vector& v){
  32. this->size = v.size;
  33. this->data = new T[this->size];
  34. for (int i = 0; i < v.size; i++){
  35. this->data[i] = v.data[i];
  36. }
  37. }
  38. friend ostream& operator<< (ostream& out, Vector<T> & v) {
  39. for (int i = 0; i < v.size; i++) {
  40. out << v[i] << " ";
  41. }
  42. out << endl;
  43. return out;
  44. }
  45. friend istream& operator>> (istream& is, Vector<T> & v);
  46.  
  47. Vector<T> operator+ (const Vector& v){
  48. Vector temp(*this);
  49. for (int i = 0; i < this->size; i++){
  50. temp.data[i] += v.data[i];
  51. }
  52. return temp;
  53. }
  54.  
  55. T& operator[] (const int i) {
  56. if (i < 0) { return data[0]; }
  57. if (i >= size) { return data[size - 1]; }
  58. return data[i];
  59. }
  60.  
  61. ~Vector(){
  62. delete[] data;
  63. }
  64. };
  65.  
  66.  
  67. template<typename T>
  68. istream& operator >> (istream& is, Vector<T> & v){
  69. for (int i = 0; i < v.size; i++) {
  70. is >> v[i];
  71. }
  72. return is;
  73. }
  74.  
  75.  
  76. int main()
  77.  
  78. {
  79. Vector<int>v1(3);
  80. Vector<int>v2(3);
  81. Vector<double>v3(3);
  82. Vector<double>v4(3);
  83. Vector<int> n ;
  84. n = v1 + v2;
  85. cout << "v1 + v2 = " << n;
  86. return 0;
  87. }
RAW Paste Data