Creative Code

boundArray(index지정) 본문

C++ Programming

boundArray(index지정)

빛하루 2023. 9. 7. 17:07

※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++ Programming' 카테고리의 다른 글

stack4(template사용)  (0) 2023.09.08
array2(template사용)  (0) 2023.09.08
shape(도형 상속)  (0) 2023.09.07
safeArray2(상속)  (0) 2023.09.07
queue3(array클래스와 has -a관계)  (0) 2023.09.06