//PFArray.h
#ifndef PFArray_H
#define PFArray_H
namespace PFArraySavitch
{
template<class T>
class PFArray
{
public:
PFArray();
PFArray(int capacityValue);
PFArray(const PFArray<T>& pfaObject);
void addElement(T element);
//선행조건: 배열은 가득차있지않다.
//사후조건: 원소가 추가되었다.
bool full() const;
int getCapacity() const;
int getNumberUsed() const;
void emptyArray();
//사용된 원소의 수를 0으로 다시 설정하여 배열을 효과적으로 비게한다.
T& operator[](int index);
PFArray<T>& operator =(const PFArray<T>& rightSide);
virtual ~PFArray();
private:
T * a; //T형의 배열을 위한 것
int capacity;// 배열크기
int used;
};
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////
//PFArray.cpp
#include<iostream>
#include"PFArray.h"
using std::cout;
namespace PFArraySavitch
{
template<class T>
PFArray<T>::PFArray() : capacity(50), used(0)
{
a = new T[capacity];
}
template<class T>
PFArray<T>::PFArray(int capacityValue) : capacity(capacityValue), used(0)
{
a = new T[capacity];
}
template<class T>
PFArray<T>::PFArray(const PFArray<T>& pfaObject)
: capacity(pfaObject.getCapacity()), used(pfaObject.getNumberUsed())
{
a = new T[capacity];
for (int i = 0; i < used; i++)
a[i] = pfaObject.a[i];
}
template<class T>
void PFArray<T>::addElement(T element)
{
if (used >= capacity)
{
cout << "Attempt to exceed capacity in PFArray.\n";
exit(1);
}
a[used] = element;
used++;
}
template<class T>
bool PFArray<T>::full() const
{
return (capacity == used);
}
template<class T>
int PFArray<T>::getCapacity() const
{
return capacity;
}
template<class T>
int PFArray<T>::getNumberUsed() const
{
return used;
}
template<class T>
void PFArray<T>::emptyArray()
{
used = 0;
}
template<class T>
T& PFArray<T>::operator[](int index)
{
if (index >= used)
{
cout << "Illegal index in PFArray.\n";
exit(1);
}
return a[index];
}
template<class T>
PFArray<T>& PFArray<T>::operator =(const PFArray<T>& rightSide)
{
if (capacity != rightSide.capacity)
{
delete[] a;
a = new T[rightSide.capacity];
}
capacity = rightSide.capacity;
used = rightSide.used;
for (int i = 0; i < used; i++)
{
a[i] = rightSide.a[i];
}
return *this;
}
template<class T>
PFArray<T>::~PFArray()
{
delete[] a;
}
}
#ifndef PFArray_H
#define PFArray_H
namespace PFArraySavitch
{
template<class T>
class PFArray
{
public:
PFArray();
PFArray(int capacityValue);
PFArray(const PFArray<T>& pfaObject);
void addElement(T element);
//선행조건: 배열은 가득차있지않다.
//사후조건: 원소가 추가되었다.
bool full() const;
int getCapacity() const;
int getNumberUsed() const;
void emptyArray();
//사용된 원소의 수를 0으로 다시 설정하여 배열을 효과적으로 비게한다.
T& operator[](int index);
PFArray<T>& operator =(const PFArray<T>& rightSide);
virtual ~PFArray();
private:
T * a; //T형의 배열을 위한 것
int capacity;// 배열크기
int used;
};
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////
//PFArray.cpp
#include<iostream>
#include"PFArray.h"
using std::cout;
namespace PFArraySavitch
{
template<class T>
PFArray<T>::PFArray() : capacity(50), used(0)
{
a = new T[capacity];
}
template<class T>
PFArray<T>::PFArray(int capacityValue) : capacity(capacityValue), used(0)
{
a = new T[capacity];
}
template<class T>
PFArray<T>::PFArray(const PFArray<T>& pfaObject)
: capacity(pfaObject.getCapacity()), used(pfaObject.getNumberUsed())
{
a = new T[capacity];
for (int i = 0; i < used; i++)
a[i] = pfaObject.a[i];
}
template<class T>
void PFArray<T>::addElement(T element)
{
if (used >= capacity)
{
cout << "Attempt to exceed capacity in PFArray.\n";
exit(1);
}
a[used] = element;
used++;
}
template<class T>
bool PFArray<T>::full() const
{
return (capacity == used);
}
template<class T>
int PFArray<T>::getCapacity() const
{
return capacity;
}
template<class T>
int PFArray<T>::getNumberUsed() const
{
return used;
}
template<class T>
void PFArray<T>::emptyArray()
{
used = 0;
}
template<class T>
T& PFArray<T>::operator[](int index)
{
if (index >= used)
{
cout << "Illegal index in PFArray.\n";
exit(1);
}
return a[index];
}
template<class T>
PFArray<T>& PFArray<T>::operator =(const PFArray<T>& rightSide)
{
if (capacity != rightSide.capacity)
{
delete[] a;
a = new T[rightSide.capacity];
}
capacity = rightSide.capacity;
used = rightSide.used;
for (int i = 0; i < used; i++)
{
a[i] = rightSide.a[i];
}
return *this;
}
template<class T>
PFArray<T>::~PFArray()
{
delete[] a;
}
}
댓글
댓글 쓰기