题解 | #数组类的构造函数#
数组类的构造函数
https://www.nowcoder.com/practice/1f36f85726474afbb103f65a24106824
#include<bits/stdc++.h>
using namespace std;
class Array{
private:
int n;//数组大小
int *a;//数组
public:
// write your code here......
Array()
{
cin >> n;
a = new int[n];
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
}
~Array(){
delete []a;
}
void show(){
for (int i=0;i<n;i++) cout<<a[i]<<' ';
}
};
int main(){
Array a;
a.show();
return 0;
}
挺巧妙的,在构造函数里面指定输入。而不是使用形参列表。
C++题解 文章被收录于专栏
记录在牛客网用C++刷题的题解思路
查看2道真题和解析