Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Конструктор копирования.
- // Оператор присваивания.
- // Константные методы.
- class CandyBox {
- int cnt; // Количество конфет
- int* candies; // Динамический массив конфет
- //int len;
- //int* data;
- public:
- int size() const {
- return cnt;
- }
- Array(int cnt = 0) {
- this->cnt = cnt;
- candies = new int[cnt];
- }
- Array(const Array &other) {
- cnt = other.size();
- candies = new int[cnt];
- for (int i = 0; i < cnt; ++i)
- candies[i] = other.candies[i];
- }
- Array& operator= (const Array &other) {
- delete[] candies;
- cnt = other.cnt;
- candies = new int[cnt];
- for (int i = 0; i < cnt; ++i)
- candies[i] = other.candies[i];
- return *this;
- }
- ~Array() {
- delete[] candies;
- }
- int at(int index) const {
- return candies[index];
- }
- int& at(int index) {
- return candies[index];
- }
- };
- Array f(Array z) {
- z.at(2) = 9;
- return z;
- }
- int main() {
- Array w(5);
- w.at(0) = 7;
- w.at(1) = 5;
- w.at(2) = -1;
- w.at(3) = 8;
- w.at(4) = 3;
- w = f(w);
- for (int i = 0; i < w.size(); ++i)
- cout << w.at(i) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement