题解 | #数组类的构造函数#
数组类的构造函数
https://www.nowcoder.com/practice/1f36f85726474afbb103f65a24106824
#include<bits/stdc++.h>
using namespace std;
class Array{
private:
int n;//数组大小
int *a;//数组
public:
Array(int num){
n = num;
a = new int[n];
//注意数组需要申请内存
for(int i = 0; i < n ; i++) cin >> a[i] ;
//cin在读入时会跳过空格。它只会读取连续的非空格字符,直到遇到下一个空格或换行符为止。如果你想读取包含空格的字符串,可以使用getline()函数。
}
~Array(){
delete []a;
}
void show(){
for (int i = 0; i < n ; i++) cout<< a[i] <<' ';
}
};
int main(){
int num;
cin >> num;
Array a(num);
a.show();
return 0;
}
顺丰集团工作强度 379人发布