c++实现int数组简单的拷贝、逆序、打印、赋值、判断相等功能

用类实现,那时还没设置vim缩进
摘要由CSDN通过智能技术生成
#include<iostream>
using namespace std;


class Array
{
public:


Array()//无参构造函数
{
this->len = 0;
this->space = NULL;
cout<<"Array()"<<endl;
}


Array(int len)//有参构造函数
{
if(len<=0)
{
this->len = 0;
this->space = NULL;
}
else
{
this->len = len;
this->space = new int[len];
cout<<"创建了一个长度为"<<len<<"的数组"<<endl;
}
}


Array(const Array &another)//拷贝构造函数,把另一个数组拷贝过来
{
if(another.len>=0)
{
this->len = another.len;
//执行深拷贝
//先给新对象在堆中创建一定长度的数组
this->space = new int[this->len];
//把原对象的值赋给新对象
for(int i =0;i<this->len;i++)
{
this->space[i] = another.space[i];
}
cout<<"调用了默认拷贝构造函数"<<endl;
}
}


~Array()//析构函数
{
if(this->space != NULL)
{
delete[] this->space;//要先释放this->space指向的空间
this->space = NULL;//再干掉space这个指针
len = 0;
}
cout<<"~Array()"<&l
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值