Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Конструктор копирования.
  5. // Оператор присваивания.
  6. // Константные методы.
  7.  
  8. class CandyBox {
  9. int cnt; // Количество конфет
  10. int* candies; // Динамический массив конфет
  11. //int len;
  12. //int* data;
  13.  
  14. public:
  15. int size() const {
  16. return cnt;
  17. }
  18.  
  19. Array(int cnt = 0) {
  20. this->cnt = cnt;
  21. candies = new int[cnt];
  22. }
  23.  
  24. Array(const Array &other) {
  25. cnt = other.size();
  26. candies = new int[cnt];
  27. for (int i = 0; i < cnt; ++i)
  28. candies[i] = other.candies[i];
  29. }
  30.  
  31. Array& operator= (const Array &other) {
  32. delete[] candies;
  33. cnt = other.cnt;
  34. candies = new int[cnt];
  35. for (int i = 0; i < cnt; ++i)
  36. candies[i] = other.candies[i];
  37. return *this;
  38. }
  39.  
  40. ~Array() {
  41. delete[] candies;
  42. }
  43. int at(int index) const {
  44. return candies[index];
  45. }
  46.  
  47. int& at(int index) {
  48. return candies[index];
  49. }
  50. };
  51.  
  52. Array f(Array z) {
  53. z.at(2) = 9;
  54. return z;
  55. }
  56.  
  57. int main() {
  58. Array w(5);
  59. w.at(0) = 7;
  60. w.at(1) = 5;
  61. w.at(2) = -1;
  62. w.at(3) = 8;
  63. w.at(4) = 3;
  64. w = f(w);
  65. for (int i = 0; i < w.size(); ++i)
  66. cout << w.at(i) << endl;
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement