学习笔记:C++数组类的封装
2021/9/23 22:11:31
本文主要是介绍学习笔记:C++数组类的封装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
MyArray.h #pragma once #include <iostream> using namespace std; class MyArray { public: MyArray(); //默认构造 默认100容量 MyArray(int capacity); MyArray(const MyArray& array); ~MyArray(); //尾插法 void push_Back(int val); //根据索引获取值 int getDate(int index); //根据索引设置值 void setDate(int index, int val); //获取数组大小 int getSize(); //获取数组大小 int getCapacity(); //[]运算符重载 int& operator[](int index); private: int* pAddress; //指向真正储存数据的指针 int m_Size; //数组大小 int m_Capacity; //数组容量 };
MyArray.cpp #include "MyArray.h" //默认构造 MyArray::MyArray() { this->m_Capacity = 100; this->m_Size = 0; this->pAddress = new int[this->m_Capacity]; } //有参构造 MyArray::MyArray(int capacity) { //cout << "有参函数调用" << endl; this->m_Capacity = capacity; this->m_Size = 0; this->pAddress = new int[this->m_Capacity]; } //拷贝构造 MyArray::MyArray(const MyArray& array) { //cout << "拷贝构造函数调用" << endl; this->pAddress = new int[array.m_Capacity]; this->m_Size = array.m_Size; this->m_Capacity = array.m_Capacity; for (int i = 0; i < array.m_Size; i++) { this->pAddress[i] = array.pAddress[i]; } } //析构 MyArray::~MyArray() { if (this->pAddress != NULL) { //cout << "析构函数调用" << endl; delete[] this->pAddress; this->pAddress = NULL; } } void MyArray::push_Back(int val) { this->pAddress[this->m_Size] = val; this->m_Size++; } int MyArray::getDate(int index) { return this->pAddress[index]; } void MyArray::setDate(int index, int val) { this->pAddress[index] = val; } int MyArray::getSize() { return this->m_Size; } int MyArray::getCapacity() { return this->m_Capacity; } //[]重载的实现 int& MyArray::operator[](int index) { return this->pAddress[index]; }
C++强化训练-数组类的封装.cpp #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include"MyArray.h" using namespace std; void test01() { //堆区创建数组 MyArray* array = new MyArray(30); MyArray* array2 = new MyArray(*array); //new的方式指定调用拷贝构造 MyArray array3 = *array; //构造函数返回的本体 //MyArray* array2(array); //这个是声明一个指针 和array执行的地址相同 //尾插法测试 for (int i = 0; i < 10; i++) { array2->push_Back(i); } //获取数据测试 for (int i = 0; i < 10; i++) { cout << array2->getDate(i) << endl; } //设置值测试 array2->setDate(0, 1000); cout << array2->getDate(0) << endl; //获取数组大小 cout << "array2的数组大小为: " << array2->getSize() << endl; //获取数组容量 cout << "array2的数组容量为: " << array2->getCapacity() << endl; //获取设置数组内容 如何用[]进行设置访问:[]运算符重载 array3.push_Back(100000); cout << array3.getDate(0) << endl; cout << array3[0] << endl; array3[0] = 100; cout << array3[0] << endl; delete array; } int main() { test01(); system("pause"); return EXIT_SUCCESS; }
这篇关于学习笔记:C++数组类的封装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升