Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- ////////////////////////////////////////////////
- class Array
- {
- private:
- public: int size;
- int *data;
- //////////////////////
- Array(int s) : size(s) // Конструктор
- {
- data = new int[size];
- for(int i = 0; i < size; i++)
- {
- data[i] = 0;
- }
- }
- ////////////////////////////////////////////
- Array(const Array& other) : size(other.size) // Конструктор глубокого копирования
- {
- cout << "Глубокое копирование!" << endl;
- data = new int[size];
- for (int i = 0; i < size; i++)
- {
- data[i] = other.data[i]; // копируем значения, а не указатели
- }
- }
- ///////
- ~Array() // Деструктор
- {
- delete[] data;
- }
- ////////////////////////////////////
- Array& operator = (const Array& other) // Оператор присваивания (для полноты)
- {
- if(this != &other)
- {
- delete[] data;
- size = other.size;
- data = new int[size];
- for(int i = 0; i < size; i++)
- {
- data[i] = other.data[i];
- }
- }
- return *this;
- }
- };
- void monitor(Array a);
- /////////////////////////////////////////////////////////////////
- int main()
- {
- setlocale(LC_ALL, "rus");
- Array a1(12), a2(7), a3(10);
- a2 = a1;
- monitor(a2);
- return 0;
- }
- //////////////////////////////////////////////////////////////////
- void monitor(Array a)
- {
- for(int i = 0; i < a.size; i ++)
- {
- cout << a.data[i] << ", ";
- } cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment