Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class IntArray
- {
- int *a;
- int n;
- public:
- IntArray(int n)
- {
- this->n = n;
- // Mặc định toàn bộ các phần tử trong mảng có giá trị 0
- a = new int[n] {};
- }
- void setValue(int index, int value)
- {
- a[index] = value;
- }
- void print()
- {
- for (int i = 0; i < n; i++)
- cout << a[i] << " ";
- cout << endl;
- }
- };
- int main()
- {
- IntArray a(3);
- IntArray b(a);
- cout << "a: ";
- a.print();
- cout << "b: ";
- b.print();
- // Thay đổi một giá trị của a
- a.setValue(1, 10);
- cout << "After change" << endl;
- cout << "a: ";
- a.print();
- cout << "b: ";
- b.print();
- // Mặc dù b.setValue(...) không được gọi nhưng giá trị của b vẫn bị thay đổi
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement