1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include <iostream>
#include <utility>
#include <algorithm>
using namespace std;
class myfunnyarray {
size_t Size {};
int* array {};
public:
myfunnyarray() {}
myfunnyarray(size_t size);
myfunnyarray(size_t size, int value);
myfunnyarray(const myfunnyarray& rhs);
~myfunnyarray();
myfunnyarray& swap(myfunnyarray& rhs);
myfunnyarray& operator=(const myfunnyarray rhs);
myfunnyarray operator+(const myfunnyarray& rhs) const;
myfunnyarray operator-(int no) const;
int& operator[](size_t index);
const int& operator[](size_t index) const;
myfunnyarray operator++(int);
myfunnyarray& operator++();
friend ostream& operator<<(ostream& lhs, const myfunnyarray& rhs);
};
myfunnyarray& myfunnyarray::swap(myfunnyarray& rhs) {
std::swap(rhs.Size, Size);
std::swap(rhs.array, array);
return *this;
}
myfunnyarray& myfunnyarray::operator=(myfunnyarray rhs) {
swap(rhs);
return *this;
}
myfunnyarray::myfunnyarray(const myfunnyarray& rhs) : Size(rhs.Size), array(new int[rhs.Size]) {
std::copy_n(rhs.array, Size, array);
}
myfunnyarray myfunnyarray::operator ++(int) {//post increment
myfunnyarray temp(*this);
for (size_t i = 0; i < Size; ++i)
++array[i];
return temp;
}
myfunnyarray& myfunnyarray::operator++() {
for (size_t i = 0; i < Size; ++i)
++array[i];
return *this;
}
myfunnyarray::myfunnyarray(size_t size) : Size(size), array(new int[size] {}) {}
myfunnyarray::myfunnyarray(size_t size, int value) : Size(size), array(new int[size]) {
std::fill_n(array, Size, value);
}
ostream& operator << (ostream& lhs, const myfunnyarray& rhs) {
for (size_t i = 0; i < rhs.Size; ++i)
lhs << rhs.array[i] << ' ';
return lhs;
}
myfunnyarray myfunnyarray ::operator+(const myfunnyarray& rhs) const {
myfunnyarray obj(rhs.Size + Size);
for (size_t ind = 0; ind < Size; ++ind)
obj.array[ind] = array[ind];
for (size_t ind = 0; ind < rhs.Size; ++ind)
obj.array[ind + Size] = rhs.array[ind];
return obj;
}
int& myfunnyarray:: operator[] (size_t index) {
if (index < Size)
return array[index];
cerr << "invalid index";
exit(-1);
}
const int& myfunnyarray:: operator[] (size_t index) const {
if (index < Size)
return array[index];
cerr << "invalid index";
exit(-1);
}
myfunnyarray myfunnyarray ::operator-(int no) const {
myfunnyarray obj (Size);
for (size_t i = 0; i < Size; ++i)
obj.array[i] = array[i] - no;
return obj;
}
myfunnyarray::~myfunnyarray() {
delete[] array;
}
bool checkifzero(myfunnyarray& arg, int index) {
return arg[index == 0];
}
int main() {
myfunnyarray a(3); // should create an array containing 3 integers each initialized to zero
cout << a << '\n'; // should print: 0,0,0
myfunnyarray b(4, 5); // should create an array containing 4 integers each initialized to five
cout << b << '\n'; // should print: 5,5,5,5
myfunnyarray c = a + b; // c should contain 7 elements: first 3 intialized to 0, last four to 5.
cout << c << '\n'; // should print: 0,0,0,5,5,5,5
c[1] = 33; // should assign value 33 to the element at index 1
if (checkifzero(c, 1) == false)
cout << c << '\n'; // should print: 0,33,0,5,5,5,5
myfunnyarray d = c++;
cout << d << '\n'; // should print: 0,33,0,5,5,5,5
cout << c << '\n'; // should print: 1,34,1,6,6,6,6
myfunnyarray e = ++(++c);
cout << e << '\n'; // should print: 3,36,3,8,8,8,8
cout << c << '\n'; // should print: 3,36,3,8,8,8,8
myfunnyarray f = c - 3; // f[i] = c[i]-3;
cout << f << '\n'; // should print: 0,33,0,5,5,5,5
cout << c << '\n'; // should print: 3,36,3,8,8,8,8//there was a mistake here,sir wrote 38 instead of36
(c = b) = a;
cout << c << '\n'; // should print: 0,0,0
cout << a << "-" << b << "-" << c << '\n'; // should print 0,0,0-5,5,5,5-0,0,0
}
|