#include <iostream>
#include <ostream>
class Array {
public:
explicit Array(const std::size_t size): size{size}, data{new int[size]} {}
Array(const std::initializer_list<int> &list): size{list.size()}, data{new int[list.size()]} {
std::copy(list.begin(), list.end(), data);
}
// rule of three:
// copy constructor
Array(const Array &other): size{other.size}, data{new int[other.size]} {
std::copy(other.data, other.data + other.size, data);
}
// copy assignment
Array &operator=(const Array &other) {
if (this != &other) {
delete[] data;
this->size = other.size;
this->data = new int[other.size];
std::copy(other.data, other.data + other.size, data);
}
return *this;
}
// destructor
virtual ~Array() {
delete[] data;
}
int operator[](std::size_t i) const {
return data[i];
}
int &operator[](std::size_t i) {
return data[i];
}
std::size_t getSize() const {
return size;
}
private:
std::size_t size;
int *data;
};
// prefix
Array &operator++(Array &array) {
for (std::size_t i = 0; i < array.getSize(); ++i) {
++array[i];
}
return array;
}
// postfix
Array operator++(Array &array, int) {
Array temp{array};
++array;
return temp;
}
// prefix
Array &operator--(Array &array) {
for (std::size_t i = 0; i < array.getSize(); ++i) {
--array[i];
}
return array;
}
// postfix
Array operator--(Array &array, int) {
Array temp{array};
--array;
return temp;
}
std::ostream &operator<<(std::ostream &out, const Array &array) {
for (std::size_t i = 0; i < array.getSize(); ++i) {
out << array[i] << " ";
}
return out;
}
std::istream &operator>>(std::istream &in, Array &array) {
for (std::size_t i = 0; i < array.getSize(); ++i) {
in >> array[i];
}
return in;
}
int main() {
Array c{1, 2, 3, 4, 5};
std::cout << c << std::endl;
std::cout << c++ << std::endl;
std::cout << c << std::endl;
std::cout << ++c << std::endl;
std::cout << c << std::endl;
std::cout << c-- << std::endl;
std::cout << c << std::endl;
std::cout << --c << std::endl;
return 0;
}
Вибачте ось код