MyArray.h
#ifndef MYARRAY_H
#define MYARRAY_H
class MyArray{
public:
MyArray();
explicit MyArray(int capacity);
void SetData(int pos, int val);
int GetData(int pos);
void PushBack(int val);
int GetLength();
~MyArray();
private:
int mCapacity;
int mSize;
int* pAdress;
};
#endif
MyArray.cpp
#include"MyArray.h"
MyArray::MyArray(){
this->mCapacity = 100;
this->mSize = 0;
this->pAdress = new int[this->mCapacity];
}
MyArray::MyArray(int capacity){
this->mCapacity = capacity;
this->mSize = 0;
this->pAdress = new int[capacity];
}
void MyArray::SetData(int pos, int val){
if (pos < 0 || pos > mCapacity - 1){
return;
}
pAdress[pos] = val;
}
int MyArray::GetData(int pos){
return pAdress[pos];
}
void MyArray::PushBack(int val){
if (mSize >= mCapacity){
return;
}
this->pAdress[mSize] = val;
this->mSize++;
}
int MyArray::GetLength(){
return this->mSize;
}
MyArray::~MyArray(){
if (this->pAdress != nullptr){
delete[] this->pAdress;
}
}
TestMyArray.cpp
#include"MyArray.h"
void test(){
MyArray myarray(50);
for (int i = 0; i < 50; i++){
myarray.PushBack(i);
}
for (int i = 0; i < myarray.GetLength(); i++){
cout << myarray.GetData(i) << " ";
}
cout << endl;
}