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

数组类的拷贝构造函数

https://www.nowcoder.com/practice/73014020b7d54622ac5000b06eaaa4ef

#include<bits/stdc++.h>
#include <cstring>
using namespace std;
class Array{
	private:
		int n;//数组大小 
		int *a;//数组 
	public:
		Array(){
			cin>>n;
			a=new int [n];
			for (int i=0;i<n;i++) cin>>a[i];
		}
		~Array(){
			delete []a;
		}
		int getlen(){
			return n;
		}
		int get(int i){
			return a[i];
		}
		// write your code here......
        Array(const Array& p)
		{
			this->n=p.n;
			this->a=new int[n];
			for(int i=0;i<n;i++)// 注意是整型数组不能用strcpy复制数组中的内容
			{
				this->a[i]=p.a[i];
			}
		}

		void show(){
			for (int i=0;i<n;i++) cout<<a[i]<<' ';
		}
};
int main(){
	Array a;
	Array b=a; 
	b.show();
	return 0;
}

#数组类的拷贝构造函数#
全部评论

相关推荐

点赞 评论 收藏
分享
04-25 19:29
已编辑
宁波大学 运营
被普调的六边形战士很高大:你我美牛孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务