c++实现一个数组类

老师上课敲的例子,修改了一个bug

1 该数组类支持在对应位置删除和插入元素

2 静态对象只能调用静态成员函数,因此[]符重载用了两个版本

类头文件

 

#pragma once
class MyVect
{
public:
	MyVect();
	MyVect(int n);
	MyVect(const MyVect& other);			//拷贝构造函数
	MyVect& operator = (const MyVect& rhs); //重载赋值符
	void push_back(double var);			
	double pop_back();
	void insert(int n, double var);
	void erase(int n);
	void reallocate();					    //重新分配内存
	void clear();
	void print();
	double& operator[](int n);				//重载[]符
	const double& operator[](int n)const;   //const成员函数,返回值是const
	virtual ~MyVect();
private:
	double* date;	   //数组指针
	int date_size;	   //数组实际大小
	int date_capacity; //数组容量
};

 

 

类源文件

 

#include "MyVect.h"
#include<iostream>
using namespace std;

MyVect::MyVect()
{
	date = nullptr;
	date_size = 0;
	date_capacity = 0;
}

MyVect::MyVect(int n)
{
	date_size = n;
	date_capacity = 2 * n + 1;
	date = new double[date_capacity];
}

double& MyVect::operator[](int n)
{
	return date[n];
}

const double& MyVect::operator[](int n) const //常成员函数供常对象调用
{
	return date[n];
}

MyVect& MyVect::operator=(const MyVect& rhs) //重载赋值符
{
	if (this == &rhs)
	{
		return *this;
	}
	date_capacity = rhs.date_capacity;
	date_size = rhs.date_size;
	if (date != nullptr)
	{
		delete[] date;
	}
	date = new double[date_capacity];
	for (int i = 0; i < rhs.date_size; i++)
	{
		date[i] = rhs.date[i];
	}
	return *this;
}

MyVect::MyVect(const MyVect& other)
{
	this->date_size = other.date_size;
	this->date_capacity = other.date_capacity;
	date = new double[date_capacity];
	for (int i = 0; i < date_size; i++)
	{
		date[i] = other.date[i];
	}
}
void MyVect::reallocate()  //重新分配内存
{
	if (date_size == date_capacity)
	{
		double* old = date;
		date_capacity = 2 * date_size + 1;
		date = new double[date_capacity];
		for (int i = 0; i < date_size; i++)
		{
			date[i] = old[i];
		}
		if (old != nullptr)
			delete[] old;
	}
}

void MyVect::push_back(double var)
{
	this->reallocate();
	date[date_size] = var;
	date_size++;
}

double MyVect::pop_back()
{
	double out = date[date_size-1];
	date_size--;
	return out;
}

void MyVect::insert(int n, double var)
{
	reallocate();
	for (int i = date_size; i >n; i--)
	{
		date[i] = date[i-1];
	}
	date[n] = var;
	date_size++;
}

void MyVect::erase(int n)
{
	for (int i = n; i < date_size; i++)
	{
		date[i] = date[i + 1];
	}
	date_size--;
}

void MyVect::clear()
{
	date_size = 0;
	date_capacity = 0;
	if (date != nullptr)
		delete [] date;
	date = nullptr; //此句不可忘,否则析构时删除野指针出现内存错误
}

void MyVect::print()
{
	for (int i = 0; i < date_size; i++)
		cout << date[i] << '\t';
	cout << endl;
}

MyVect::~MyVect()
{
	date_size = 0;
	date_capacity = 0;
	if (date!=nullptr)
	{
		delete[] date;
	}
	date = nullptr;
	//cout << "析构啦!" << endl;
}

 

 

 

 

 

 

 

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值