1 #include<iostream> 2 using namespace std; 3 class test 4 { 5 int *p; 6 int pn; 7 public: 8 test(int n) { p = new int[n]; pn = n; } 9 void setp(); 10 int* getp() { return p; } 11 int getpn() { return pn; } 12 }; 13 void test::setp() 14 { 15 int choice; 16 cout << "1:自动填充 2:手动填充" << endl; 17 cin >> choice; 18 switch (choice) 19 { 20 case 1: 21 for (int i = 0; i < pn; i++) 22 p[i] = i; 23 break; 24 case 2: 25 for (int i = 0; i < pn; i++) 26 cin>>i; 27 break; 28 } 29 } 30 void main() 31 { 32 test A(4); 33 A.setp(); 34 int*b = new int[A.getpn()]; 35 b = A.getp(); 36 for (int i = 0; i < A.getpn(); i++) 37 cout << b[i] << endl; 38 }