Creative Code

safeArray2(상속) 본문

C++ Programming

safeArray2(상속)

빛하루 2023. 9. 7. 11:32

※main.cpp파일

#include <iostream>

#include "safeArray.h"

#include "array.h"



int main() {



	int nums[] = {1,2,3,4,5};



	Array arr(nums,5);

	arr[5] = 6;

	

	//SafeArray arr1(nums,5);

	//arr1[5] = 6;	

	

	//polymorphism

	Array *pArr = new SafeArray(nums,5); 

	// 부모클래스 타입의 포인터로 자식클래스의 객체를 가리키거나 참조할 수 있다.(상속관계)  

	(*pArr)[5] = 6;	  // pArr->operator[](5)  ->역참조했을때 SafeArray가 아닌 Array객체가 호출된다.

	delete pArr;

	

	Array &rArr = arr;

	

	

	return 0;

}

※safeArray.h파일

 

#ifndef SAFEARRAY_H

#define SAFEARRAY_H

#include "array.h"



class SafeArray:public Array { // array클래스로부터 상속

private:

	

public:

	explicit SafeArray(int size = Array::getDefaultSize());

	SafeArray(const int *pArr, int size);

	//SafeArray(const SafeArray& rhs);

	virtual ~SafeArray() {} //가상함수가 1개이상인 클래스의 경우에는 소멸자가 virtual형태가 되어야한다.

	

	//SafeArray& operator=(const SafeArray& rhs);

	bool operator==(const SafeArray& rhs) const;

	

	virtual int& operator[](int index);    //function overriding(부모쪽에 있는 멤버함수를 자식클래스에서 재정의할때)

	virtual const int& operator[](int index)const;

};



#endif

※ safeArray.cpp파일

#include "safeArray.h"

#include <cassert>



SafeArray::SafeArray(int size) 

:Array(size)

{



}



SafeArray::SafeArray(const int *pArr, int size)

:Array(pArr,size)

{



}



/*

SafeArray::SafeArray(const SafeArray& rhs)

:Array((Array)rhs) // slicing

{



}



SafeArray::~SafeArray() {

}



SafeArray& SafeArray::operator=(const SafeArray& rhs) {

	this->Array::operator=((Array)rhs);

	return *this;

}

*/

bool SafeArray::operator==(const SafeArray& rhs) const {

	return this->Array::operator==((Array)rhs);

}



int& SafeArray::operator[](int index) {

	assert(index >= 0 && index < this->Array::size_);

	return this->Array::operator[](index);

}



const int& SafeArray::operator[](int index) const{

	assert(index >= 0 && index < this->Array::size_);

	return this->Array::operator[](index);

}

※array.h파일

#ifndef ARRAY_H

#define ARRAY_H



//extern const int ARRAYSIZE; //main.cpp에서 다른 파일의 전역변수에 접근할 때는 extern을 써줘야한다.



class Array {

private:

	static const int ARRAYSIZE; 

	// 전역변수이지만 class와 관련된 전역변수는 static으로 써 클래스 안에 둘 수 있다.

	//private에 두면 main에서는 접근불가능

	//public에 두면 main에서 접근가능

protected: // 자식쪽에 있는 멤버함수가 부모의 private멤버에 접근하고싶을 때 protected변수로 지정해준다.

	int *pArr_;

	int size_;

public:

	//Array *operator&() const {return this;}

	//const Array *operator&() const {return this;}

	

	static int getDefaultSize();  // static이 붙었기 때문에 멤버함수가 아니라 전역함수이다.

	

	//Array();

	explicit Array(int size = ARRAYSIZE);

	//인자가 1개있는 생성자에서 explicit을 쓰면 명시적으로 설정해주면 Array arr = 100; 처럼 배열을 생성할 수 없다.

	//반드시 Array arr(100);의 형태로 써야한다.

	Array(const Array& rhs);

	Array(const int *pArr, int size);

	virtual ~Array();

	

	Array& operator=(const Array& rhs);

	

	bool operator==(const Array& rhs) const;

	

	virtual int& operator[](int index);   // 동적바인딩

	virtual const int& operator[](int index) const;

	

	int size() const;

};

#endif

※array.cpp파일

#include <cassert>

#include "array.h"



//const int ARRAYSIZE = 100; //constant (vs. literal)

const int Array::ARRAYSIZE = 100;

/*Array::Array() 

:pArr_(new int[ARRAYSIZE]),size_(ARRAYSIZE)  //생성자 초기화 리스트 (콜론을 사용해 표시)- 생성자에서만 쓸 수 있음

{

	//pArr_ = new int[ARRAYSIZE];

	assert(pArr_);

	//size_ = ARRAYSIZE;

}

*/



int Array::getDefaultSize() {

	return Array::ARRAYSIZE;

}



Array::Array(int size) 

: pArr_(new int[size]),size_(size)

{

	//pArr_ = new int[size];

	assert(pArr_);

	//size_ = size;

}



Array::Array(const Array& rhs) 

:pArr_(new int[rhs.size_]),size_(rhs.size_)

{

	//pArr_ = new int[rhs.size_];

	assert(pArr_);

	//size_ = rhs.size_;

	for (int i = 0; i<rhs.size(); i++) {

		pArr_[i] = rhs.pArr_[i];

	}

}



Array::Array(const int *pArr,int size) 

:pArr_(new int[size]),size_(size)

{

	//pArr_ = new int[size];

	assert(pArr_);

	//size_ = size;

	

	for (int i = 0; i<size; ++i) {

		pArr_[i] = pArr[i];

	}

}



Array::~Array() {

	delete[] pArr_;

}



Array& Array::operator=(const Array& rhs) {

	if (this != &rhs) {

		delete[] pArr_;

		pArr_ = new int[rhs.size_];

		assert(pArr_);

		size_ = rhs.size_;

		for (int i = 0; i<rhs.size_; ++i) {

			pArr_[i] = rhs.pArr_[i];

		}

	}

	return *this;

}



bool Array::operator==(const Array& rhs) const {

	if (size_ != rhs.size_) 

		return false;

	int i;

	for (i = 0; i<rhs.size_; ++i) {

		if (pArr_[i] != rhs.pArr_[i]) {

			break;

		}

	}

	

	return (i == rhs.size_);

}



int& Array::operator[](int index) {

	return pArr_[index];

}



const int& Array::operator[](int index)const {

	return pArr_[index];

}



int Array::size() const {

	return size_;

}

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

boundArray(index지정)  (0) 2023.09.07
shape(도형 상속)  (0) 2023.09.07
queue3(array클래스와 has -a관계)  (0) 2023.09.06
queue2(생성자 초기화 리스트, static member, explicit)  (0) 2023.09.06
safeArray(배열 크기)  (0) 2023.09.06