Recent Posts
printf("ho_tari\n");
BoundArray (index 지정) 본문
<main.cpp>
#include <iostream>
#include "boundArray.h"
int main() {
BoundArray arr1; //BoundArray arr1(0,10); [0,10)
BoundArray arr2(100); // BoundArray arr2(0,100); [0,100)
BoundArray arr3(1,11); // index[1,11)
for (int i = arr2.lower(); i<arr2.upper(); ++i) {
arr2[i] = i;
}
const BoundArray arr4 = arr3;
arr1 = arr3;
arr1 == arr3;
for (int i = arr2.lower(); i<arr2.upper(); ++i) {
std::cout << arr2[i] << " ";
}
std::cout << std::endl;
return 0;
}
<boundArray.h>
#ifndef BOUNDARRAY_H
#define BOUNDARRAY_H
#include "safeArray.h"
class BoundArray:public SafeArray {
private:
int low_;
protected:
public:
explicit BoundArray(int size= Array::getDefaultSize());
BoundArray(int low, int up);
virtual ~BoundArray(){}
bool operator==(const BoundArray&) const;
int lower();
int upper();
virtual int& operator[](int index);
virtual const int& operator[](int index) const;
};
#endif
<boundArray.cpp>
#include "boundArray.h"
#include <cassert>
BoundArray::BoundArray(int size)
:low_(0),SafeArray(size)
{
}
BoundArray::BoundArray(int low, int up)
:low_(low),SafeArray(up-low)
{
}
bool BoundArray::operator==(const BoundArray& rhs) const {
return low_ == rhs.low_ && this->SafeArray::operator==((SafeArray)rhs);
}
int& BoundArray::operator[](int index) {
return this->SafeArray::operator[](index - low_);
}
const int& BoundArray::operator[](int index) const {
return this->SafeArray::operator[](index - low_);
}
int BoundArray::lower() {
return low_;
}
int BoundArray::upper() {
return low_ + this->Array::size();
}
'C++' 카테고리의 다른 글
Stack4 (template 지정) (0) | 2023.09.08 |
---|---|
Array2 (template 지정) (0) | 2023.09.08 |
Shape (RTTI : Run Time Type Identification) (0) | 2023.09.07 |
Shape (Shape, Rectangle, Circle 상속) (0) | 2023.09.07 |
SafeArray2 (상속) (0) | 2023.09.07 |