Arkanium77

Untitled

Mar 29th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. // Объявление класса вектор для целых чисел
  10.  
  11. class Vector {
  12.     int *v; // массив координат
  13.     int n; //размерность пространства
  14. public:
  15.     Vector(int n = 2);
  16.     Vector(int *m, int n = 2);
  17.     Vector(int x, int y);
  18.     friend ostream& operator<<(ostream& os, const Vector& vi);
  19.     Vector(); // конструктор
  20.     ~Vector(); // деструктор
  21.     Vector random(); // случайные координаты
  22.     Vector sum(Vector v1); // сложение векторов
  23. };
  24. //конструктор
  25.  
  26. Vector::Vector(int n) {
  27.     //cout << "Конструктор. Инициализация объекта" << endl;
  28.     v = new int(n);
  29.     if (!v) {
  30.         cout << "Ошибка выделения памяти\n";
  31.         exit(1);
  32.     }
  33. }
  34.  
  35. Vector::Vector(int *m, int n) {
  36.     //cout << "Конструктор. Инициализация объекта" << endl;
  37.     v = new int(n);
  38.     if (!v) {
  39.         cout << "Ошибка выделения памяти\n";
  40.         exit(1);
  41.     }
  42.  
  43.     for (int i = 0; i < n; i++) {
  44.         v[i] = m[i];
  45.     }
  46. }
  47.  
  48. Vector::Vector() {
  49.     v = new int(2);
  50.     /*if (!v) {
  51.  
  52.         cout << "Ошибка выделения памяти\n";
  53.         exit(1);
  54.     }*/
  55.     v[0] = 0;
  56.     v[1] = 0;
  57. }
  58.  
  59. Vector::Vector(int x, int y) {
  60.     //cout << "Конструктор. Инициализация объекта" << endl;
  61.     v = new int(2);
  62.     if (!v) {
  63.  
  64.         cout << "Ошибка выделения памяти\n";
  65.         exit(1);
  66.     }
  67.     v[0] = x;
  68.     v[1] = y;
  69. }
  70. // деструктор
  71.  
  72. Vector::~Vector() {
  73.     //cout << "Деструктор. Освобождение памяти, занимаемой объектом" << endl;
  74.  
  75.     delete [] v;
  76. }
  77.  
  78. Vector Vector::random() {
  79.     srand(time(0));
  80.  
  81.     for (int i = 0; i < n; i++) {
  82.         v[i] = rand() % 200 - 100;
  83.     }
  84.     return *this;
  85. }
  86.  
  87. Vector Vector::sum(Vector v1) {
  88.     Vector sum(this->n);
  89.     if (v1.n != this->n) exit(1);
  90.     for (int i = 0; i < n; i++) {
  91.         sum.v[i] = v[i] + v1.v[i];
  92.     }
  93.     return sum;
  94. }
  95.  
  96. ostream& operator<<(ostream& os, const Vector& vi) {
  97.     string c = "";
  98.     for (int i = 0; i < vi.n; i++) {
  99.         c.append(vi.v[i] + " ");
  100.     }
  101.     os << c;
  102.     return os;
  103. }
  104.  
  105.  
  106. //======================================================================
  107.  
  108. int main() {
  109.     setlocale(0, "");
  110.     cout << "Uberstek  Шушу" << endl;
  111.     Vector v(6);
  112.     v = v.random();
  113.     cout << endl << "!" << endl << v;
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment