Creative Code

boundArray2(template사용) 본문

C++ Programming

boundArray2(template사용)

빛하루 2023. 9. 8. 15:11

※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

'C++ Programming' 카테고리의 다른 글

safeArray4(try-throw-catch)  (0) 2023.09.08
swap2(template 함수)  (0) 2023.09.08
safeArray3(template 사용)  (0) 2023.09.08
queue4(template사용)  (0) 2023.09.08
stack4(template사용)  (0) 2023.09.08