C++実装配列クラステンプレート
40438 ワード
このコードは黒馬さんの授業の内容に基づいて手で引き裂いたものです.特に黒馬さんの内容にはクラス配列クラスの設計が含まれていて、その中には構造関数、解析関数、コピー関数、operator再定義=オペレータ、および尾挿しと尾削除法の運用があります.
hpp
#pragma once
#ifndef MyArray
#include
#include
#include
using namespace std;
template<class T>
class MyArray
{
public:
MyArray(int Capacity)//
{
//cout << " " << endl;
this->m_Capacity = Capacity;
this->m_Size = 0;
this->m_Address = new T[this->m_Capacity];
cout << " :" << endl;
cin >> this->m_Size;
for (int i = 0; i < this->m_Size; i++)
{
this->m_Address[i] = i;
cout << this->m_Address[i] << endl;
}
}
MyArray(const MyArray& arr)//
{
//cout << " " << endl;
this->m_Size = arr.m_Size;
this->m_Capacity = arr.m_Capacity;
this->m_Address = new T[this->m_Capacity];
for (int i = 0; i < this->m_Size; i++)
{
m_Address[i] = arr.m_Address[i];
cout << this->m_Address[i] << endl;
}
}
MyArray& operator=(const MyArray& arr) //operator =
{
//cout << " operator " << endl;
if (this->m_Address != NULL)
{
delete[]this->m_Address;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Size = arr.m_Size;
this->m_Capacity = arr.m_Capacity;
this->m_Address = new T[this->m_Capacity];
for (int j = 0; j < this->m_Size; j++)
{
this->m_Address[j] = arr.m_Address[j];
cout << this->m_Address[j] << endl;
}
return *this;
}
//
MyArray& Push_Back(MyArray& arr)
{
if (this->m_Capacity == this->m_Size)
{
cout << " " << endl;
}
else
{
cout << " :" << endl;
T temp;
cin >> temp;
this->m_Address[m_Size] = temp;
this->m_Size++;
}
return *this;
}
//
MyArray& Pop_Back(MyArray& arr)
{
if (this->m_Size == 0)
{
cout << " " << endl;
}
else
{
this->m_Size--;
}
return *this;
}
void DisplayArr(MyArray& arr)
{
for (int i = 0; i < this->m_Size; i++)
{
cout << this->m_Address[i] << endl;
}
}
~MyArray()//
{
//cout << " " << endl;
if (this->m_Address != NULL)
{
delete[]this->m_Address;
}
this->m_Address = NULL;
}
private:
T* m_Address;
int m_Capacity;
int m_Size;
};
#endif // !MyArray
#pragma once
#ifndef MyArray
#include
#include
#include
using namespace std;
template<class T>
class MyArray
{
public:
MyArray(int Capacity)//
{
//cout << " " << endl;
this->m_Capacity = Capacity;
this->m_Size = 0;
this->m_Address = new T[this->m_Capacity];
cout << " :" << endl;
cin >> this->m_Size;
for (int i = 0; i < this->m_Size; i++)
{
this->m_Address[i] = i;
cout << this->m_Address[i] << endl;
}
}
MyArray(const MyArray& arr)//
{
//cout << " " << endl;
this->m_Size = arr.m_Size;
this->m_Capacity = arr.m_Capacity;
this->m_Address = new T[this->m_Capacity];
for (int i = 0; i < this->m_Size; i++)
{
m_Address[i] = arr.m_Address[i];
cout << this->m_Address[i] << endl;
}
}
MyArray& operator=(const MyArray& arr) //operator =
{
//cout << " operator " << endl;
if (this->m_Address != NULL)
{
delete[]this->m_Address;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Size = arr.m_Size;
this->m_Capacity = arr.m_Capacity;
this->m_Address = new T[this->m_Capacity];
for (int j = 0; j < this->m_Size; j++)
{
this->m_Address[j] = arr.m_Address[j];
cout << this->m_Address[j] << endl;
}
return *this;
}
//
MyArray& Push_Back(MyArray& arr)
{
if (this->m_Capacity == this->m_Size)
{
cout << " " << endl;
}
else
{
cout << " :" << endl;
T temp;
cin >> temp;
this->m_Address[m_Size] = temp;
this->m_Size++;
}
return *this;
}
//
MyArray& Pop_Back(MyArray& arr)
{
if (this->m_Size == 0)
{
cout << " " << endl;
}
else
{
this->m_Size--;
}
return *this;
}
void DisplayArr(MyArray& arr)
{
for (int i = 0; i < this->m_Size; i++)
{
cout << this->m_Address[i] << endl;
}
}
~MyArray()//
{
//cout << " " << endl;
if (this->m_Address != NULL)
{
delete[]this->m_Address;
}
this->m_Address = NULL;
}
private:
T* m_Address;
int m_Capacity;
int m_Size;
};
#endif // !MyArray
****************************************************************************
cpp
#include "MyArray.hpp"
int main()
{
MyArray<int> arr1(5);
arr1.Push_Back(arr1);
arr1.DisplayArr(arr1);
arr1.Pop_Back(arr1);
arr1.DisplayArr(arr1);
/*MyArray arr2(arr1);
MyArray arr3(10);
arr3 = arr1;*/
return 0;
}