c++:自定义数组类封装

目录

1.数组类封装目的

2.步骤

2.1首先建立一个数组类:MyArray

2.2先大致确定模版内容

2.3介绍几个基本的接口

2.4在头文件中声明以上函数接口

2.4在.c文件中实现头文件中的函数并在首句引用头文件

2.5最后实现main函数

3.增加强度:操作符重载

4.文件截图


1.数组类封装目的

解决实际问题,训练构造函数,copy构造函数,为操作符重载做铺垫

2.步骤

2.1首先建立一个数组类:MyArray

2.2先大致确定模版内容

// 根据上述需求,可以大致确定类模板中的内容

类模板{
	public:
		无参构造
        有参构造
    	拷贝构造函数
        重载operator=
        遍历函数
        赋值函数
        获得元素个数
        析构函数
  	private:
		指针
    	元素个数          
}

2.3介绍几个基本的接口

  • 无参构造
  • 有参构造
  • 析构函数
  • 赋值函数
  • 遍历函数
  • 拷贝操作
  • 访问数组长度的函数
  • 重载operator=

2.4在头文件中声明以上函数接口

#pragma once
#include<iostream>
using namespace std;
class MyArray
{
public:
	MyArray();
	MyArray(int len);
	~MyArray();
	MyArray(const MyArray1& another);
	int getLen();
	void setData(int index, int data);
	int getData(int index);
	void operator=(const MyArray& another);
private:
	int len;
	int* space;
};

2.4在.c文件中实现头文件中的函数并在首句引用头文件

#include "MyArray.h"
MyArray::MyArray()//无参构造
{
	cout << "MyArray::MyArray()..." << endl;
	this->len = 0;
	this->space = NULL;
}
MyArray::MyArray(int len)//有参构造
{
	if (len <= 0)
	{
		this->len = 0;
		return;
	}
	else
	{
		this->len = len;

		//给space开辟空间
		this->space = new int[this->len];
		cout << "MyArray::MyArray(int)..." << endl;
	}
}

MyArray::MyArray(const MyArray& 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 << "MyArray::MyArray(const MyArray& another)..." << endl;
	}
}
void MyArray::setData(int index, int data)//赋值函数
{
	if (this->space != NULL)
	{
		this->space[index] = data;
	}
	
}
int MyArray::getData(int index)//遍历返回数组值
{
	return this->space[index];
}
int MyArray::getLen()//
{
	return this->len;
}
MyArray::~MyArray()
{
	if (this->space != NULL)
	{
		delete[] space;
		this->space = NULL;
	}
	this->len = 0;
	cout << "MyArray::~MyArray()..." << endl;
}
void MyArray::operator=(const MyArray& 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 << "MyArray::MyArray::operator=(const MyArray& another)..." << endl;
	}
}

2.5最后实现main函数

#include<iostream>
#include"MyArray.h"
using namespace std;
int main()
{
  MyArray array1(10);//调用有参构造,开辟十个元素的数组
  
  //赋值操作
  for(int i=0;i<array1.getLen();i++)
  {
    array1.setData(i,i+10);
    array[i]=i+10;
  }
  cout<<"array1:"<<endl;
  
  //遍历操作
   for(int i=0;i<array1.getLen();i++)
  {
    cout<<array1.getData(i)<<" ";
  }
  cout<<endl;
  
  //拷贝操作
  MyArray array2=array1;//调用拷贝函数
  cout<<"array2:"<<endl;
  //遍历array2数组
   for(int i=0;i<array2.getLen();i++)
  {
    cout<<array2.getData(i)<<" ";
  }
  cout<<endl;
  
  //等号操作符
  MyArray array3;
  array3=array1;
  
  //遍历array3数组
   for(int i=0;i<array3.getLen();i++)
  {
    cout<<array3.getData(i)<<" ";
  }
  cout<<endl;
  return 0;
}

3.增加强度:操作符重载

  • 等号操作符重载
  • 下标引用操作符重载
  • 左移操作符重载
  • 右移操作符重载
MyArray& MyArray::operator=(const MyArray& another)
{
	if (this == &another)
	{
		return *this;
	}
	if (this->space != NULL)
	{
		delete[] this->space;
		this->space = NULL;
		this->len = 0;
	}
	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 << "MyArray::MyArray operator(const MyArray& another)..." << endl;
	}
	return *this;
}
int& MyArray::operator[](int index)const
{
	return this->space[index];
}
ostream& operator<<(ostream& os,const MyArray& array)//该函数只是用来遍历整个数组的,不希望数组中的值被改变,所以array应该加const修饰,但是当array数组加const修饰后,该数组中的成员函数也要用const修饰
{
	os << "遍历整个数组" << endl;
	array.getLen();//getLen(&array);
	for (int i = 0; i < array.getLen(); i++)
	{
		os << array[i] << " ";//array.operator[](&array,i)
	}
	os << endl;
	os << "调用的<<操作符重载" << endl;
	return os;
}
istream& operator>>(istream& is, MyArray& array)
{
	cout << "请输入" << array.getLen() << "个数" << endl;
	for (int i = 0; i < array.getLen(); i++)
	{
		cin >> array[i];
	}
	return is;
}

大家可以根据操作符重构函数自己想一想main函数和.c文件中的变化

4.文件截图

main函数:

头文件:

.c文件:

以上就是关于数组类的c++实现

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
c自定义的队列是一种数据结构,它遵循先进先出(FIFO)的原则。这个可以适用于各种应用场景,例如任务调度、缓存管理等。 这个自定义的队列具有一些基本的属性和方法,以实现队列的操作。首先,它包含一个数组或链表,用于存储数据。还有两个指针,一个指向队列的头部,另一个指向队列的尾部。 队列的主要方法包括入队(enqueue)和出队(dequeue)。入队操作将一个元素添加到队列的尾部,出队操作则删除并返回队列头部的元素。此外,还可以实现一些其他的方法,如判断队列是否为空(isEmpty)、获取队列的长度(size)等。 在队列中,还可以实现一些其他的辅助方法。例如,获取队列头部的元素(front)、获取队列尾部的元素(rear)、清空队列(clear)等。这些方法可以根据具体的需求进行定义和实现。 自定义的队列应该具有良好的封装性和可复用性。它可以提供一些公共方法,供其他程序进行调用和使用。同时,也可以考虑对队列的容量进行限制,以避免对内存资源的过度占用。 总而言之,c自定义的队列是一种功能强大、可扩展性高的数据结构。通过合理设计和实现,它可以提供一些基本的队列操作,满足不同场景下的需要。同时,这个也可以根据具体需求进行适应和扩展,以满足更为复杂的应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写不出bug的小李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值