Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
- #include <ctime>
- using namespace std;
- // Объявление класса вектор для целых чисел
- class Vector {
- int *v; // массив координат
- int n; //размерность пространства
- public:
- Vector(int n = 2);
- Vector(int *m, int n = 2);
- Vector(int x, int y);
- friend ostream& operator<<(ostream& os, const Vector& vi);
- Vector(); // конструктор
- ~Vector(); // деструктор
- Vector random(); // случайные координаты
- Vector sum(Vector v1); // сложение векторов
- };
- //конструктор
- Vector::Vector(int n) {
- //cout << "Конструктор. Инициализация объекта" << endl;
- v = new int(n);
- if (!v) {
- cout << "Ошибка выделения памяти\n";
- exit(1);
- }
- }
- Vector::Vector(int *m, int n) {
- //cout << "Конструктор. Инициализация объекта" << endl;
- v = new int(n);
- if (!v) {
- cout << "Ошибка выделения памяти\n";
- exit(1);
- }
- for (int i = 0; i < n; i++) {
- v[i] = m[i];
- }
- }
- Vector::Vector() {
- v = new int(2);
- /*if (!v) {
- cout << "Ошибка выделения памяти\n";
- exit(1);
- }*/
- v[0] = 0;
- v[1] = 0;
- }
- Vector::Vector(int x, int y) {
- //cout << "Конструктор. Инициализация объекта" << endl;
- v = new int(2);
- if (!v) {
- cout << "Ошибка выделения памяти\n";
- exit(1);
- }
- v[0] = x;
- v[1] = y;
- }
- // деструктор
- Vector::~Vector() {
- //cout << "Деструктор. Освобождение памяти, занимаемой объектом" << endl;
- delete [] v;
- }
- Vector Vector::random() {
- srand(time(0));
- for (int i = 0; i < n; i++) {
- v[i] = rand() % 200 - 100;
- }
- return *this;
- }
- Vector Vector::sum(Vector v1) {
- Vector sum(this->n);
- if (v1.n != this->n) exit(1);
- for (int i = 0; i < n; i++) {
- sum.v[i] = v[i] + v1.v[i];
- }
- return sum;
- }
- ostream& operator<<(ostream& os, const Vector& vi) {
- string c = "";
- for (int i = 0; i < vi.n; i++) {
- c.append(vi.v[i] + " ");
- }
- os << c;
- return os;
- }
- //======================================================================
- int main() {
- setlocale(0, "");
- cout << "Uberstek Шушу" << endl;
- Vector v(6);
- v = v.random();
- cout << endl << "!" << endl << v;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment