- 强化训练-数组类封装
- 设计类 myArray
- 属性
- int m_Capacity数组容量
- int m_Size 数组大小
- int pAddress 维护真实在堆区创建的数组指针
- 行为
- 默认构造
- 有参构造
- 拷贝构造
- 析构
- 根据位置 设置数据
- 根据位置 获取数据
- 尾插
- 获取数组容量
- 获取数组大小
myArray.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyArray
{
public:
MyArray(); //默认构造 可以给100容量
MyArray(int capacity); //有参构造
MyArray(const MyArray & arr); //拷贝构造
//尾插法
void pushBack(int val);
//根据位置设置数据
void setData(int pos, int val);
//根据位置获取数据
int getData(int pos);
//获取数组容量
int getCapcity();
//获取数组大小
int getSize();
//析构
~MyArray();
//重载[]运算符
int& operator[](int index);
private:
int m_Capacity; //数组容量
int m_Size; //数组大小
int *pAddress; //真实在堆区开辟的数组的指针
};
myArray.cpp
#include "myArray.h"
MyArray::MyArray()
{
cout << "默认构造函数调用" << endl;
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 & arr)
{
cout << "拷贝构造函数调用" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//this->pAddress = arr.pAddress;
this->pAddress = new int[arr.m_Capacity];
for (int i = 0; i < m_Size;i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
//尾插法
void MyArray::pushBack(int val)
{
this->pAddress[this->m_Size] = val;
this->m_Size++;
}
//根据位置设置数据
void MyArray::setData(int pos, int val)
{
this->pAddress[pos] = val;
}
//根据位置获取数据
int MyArray::getData(int pos)
{
return this->pAddress[pos];
}
//获取数组容量
int MyArray::getCapcity()
{
return this->m_Capacity;
}
//获取数组大小
int MyArray::getSize()
{
return this->m_Size;
}
//析构
MyArray::~MyArray()
{
if (this->pAddress != NULL)
{
cout << "析构函数调用" << endl;
delete [] this->pAddress;
this->pAddress = NULL;
}
}
int& MyArray::operator[](int index)
{
return this->pAddress[index];
}
强化训练-数组类封装.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "myArray.h"
void test01()
{
MyArray arr;
for (int i = 0; i < 10;i++)
{
arr.pushBack(i);
}
for (int i = 0; i < arr.getSize(); i ++)
{
cout << arr.getData(i) << endl;
}
MyArray arr2(arr);
for (int i = 0; i < arr2.getSize(); i++)
{
cout << arr2.getData(i) << endl;
}
arr.setData(0, 1000);
cout << "arr 0号位置数据为: " << arr.getData(0) << endl;
for (int i = 0; i < 10; i++)
{
cout << arr.getData(i) << endl;
}
cout << "数组容量为: " << arr.getCapcity() << endl;
cout << "数组大小为: " << arr.getSize() << endl;
//小任务: 利用[]方式去索引数组中的元素,可读可写
cout << "---------------------" << endl;
arr[0] = 10000;
cout << arr[0] << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "myArray.h"
void test01()
{
MyArray arr;
for (int i = 0; i < 10;i++)
{
arr.pushBack(i);
}
for (int i = 0; i < arr.getSize(); i ++)
{
cout << arr.getData(i) << endl;
}
MyArray arr2(arr);
for (int i = 0; i < arr2.getSize(); i++)
{
cout << arr2.getData(i) << endl;
}
arr.setData(0, 1000);
cout << "arr 0号位置数据为: " << arr.getData(0) << endl;
for (int i = 0; i < 10; i++)
{
cout << arr.getData(i) << endl;
}
cout << "数组容量为: " << arr.getCapcity() << endl;
cout << "数组大小为: " << arr.getSize() << endl;
//小任务: 利用[]方式去索引数组中的元素,可读可写
cout << "---------------------" << endl;
arr[0] = 10000;
cout << arr[0] << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}