题解 | #数组类的构造函数#

数组类的构造函数

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;
}

全部评论

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务