Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class IntArray
  6. {
  7. int *a;
  8. int n;
  9. public:
  10. IntArray(int n)
  11. {
  12. this->n = n;
  13. // Mặc định toàn bộ các phần tử trong mảng có giá trị 0
  14. a = new int[n] {};
  15. }
  16.  
  17. void setValue(int index, int value)
  18. {
  19. a[index] = value;
  20. }
  21.  
  22. void print()
  23. {
  24. for (int i = 0; i < n; i++)
  25. cout << a[i] << " ";
  26. cout << endl;
  27. }
  28. };
  29.  
  30.  
  31. int main()
  32. {
  33. IntArray a(3);
  34. IntArray b(a);
  35.  
  36. cout << "a: ";
  37. a.print();
  38. cout << "b: ";
  39. b.print();
  40.  
  41. // Thay đổi một giá trị của a
  42. a.setValue(1, 10);
  43.  
  44. cout << "After change" << endl;
  45. cout << "a: ";
  46. a.print();
  47. cout << "b: ";
  48. b.print();
  49.  
  50. // Mặc dù b.setValue(...) không được gọi nhưng giá trị của b vẫn bị thay đổi
  51.  
  52. system("pause");
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement