Recent Posts
printf("ho_tari\n");
BoundArray2 (template 사용) 본문
<main.cpp>
#include <iostream>
#include "boundArray.h"
int main() {
BoundArray<int> arr1; //BoundArray arr1(0,10); [0,10)
BoundArray<int> arr2(100); // BoundArray arr2(0,100); [0,100)
BoundArray<int> arr3(1,11); // index[1,11)
for (int i = arr3.lower(); i<arr3.upper(); ++i) {
arr3[i] = i;
}
const BoundArray<int> arr4 = arr3;
arr1 = arr3;
arr1 == arr3;
for (int i = arr3.lower(); i<arr3.upper(); ++i) {
std::cout << arr3[i] << " ";
}
std::cout << std::endl;
return 0;
}
<boundArray.h>
#ifndef BOUNDARRAY_H
#define BOUNDARRAY_H
#include "safeArray.h"
#include <cassert>
template <typename T>
class BoundArray:public SafeArray<T> {
private:
int low_;
protected:
public:
explicit BoundArray(int size= Array<T>::getDefaultSize());
BoundArray(int low, int up);
virtual ~BoundArray(){}
bool operator==(const BoundArray<T>&) const;
int lower();
int upper();
virtual T& operator[](int index);
virtual const T& operator[](int index) const;
};
template <typename T>
BoundArray<T>::BoundArray(int size)
:low_(0),SafeArray<T>(size)
{
}
template <typename T>
BoundArray<T>::BoundArray(int low, int up)
:low_(low),SafeArray<T>(up-low)
{
}
template <typename T>
bool BoundArray<T>::operator==(const BoundArray<T>& rhs) const {
return low_ == rhs.low_ && this->SafeArray<T>::operator==((SafeArray<T>)rhs);
}
template <typename T>
T& BoundArray<T>::operator[](int index) {
return this->SafeArray<T>::operator[](index - low_);
}
template <typename T>
const T& BoundArray<T>::operator[](int index) const {
return this->SafeArray<T>::operator[](index - low_);
}
template <typename T>
int BoundArray<T>::lower() {
return low_;
}
template <typename T>
int BoundArray<T>::upper() {
return low_ + this->Array<T>::size();
}
#endif
<compile 결과>
'C++' 카테고리의 다른 글
GenderRatio (static_cast) (0) | 2023.09.08 |
---|---|
SafeArray4 (try, throw, catch) (0) | 2023.09.08 |
SafeArray3 (template 사용) (0) | 2023.09.08 |
Swap2 (template 사용) (0) | 2023.09.08 |
Queue4 (template 지정) (0) | 2023.09.08 |