Recent Posts
printf("ho_tari\n");
SafeArray2 (상속) 본문
<main.cpp>
#include <iostream>
#include "safeArray.h"
#include "array.h"
int main()
{
int nums[] = {1, 2, 3, 4, 5};
//Array arr(nums, 5);
SafeArray arr(nums, 5);
//arr[5] = 6;
// polymorphism
Array *pArr = new SafeArray(nums, 5);
(*pArr)[5] = 6; // pArr->operator[](5)
delete pArr;
Array &rArr = arr;
return 0;
}
<safeArray.h>
#ifndef SAFEARRAY_H
#define SAFEARRAY_H
#include "array.h"
class SafeArray : public Array {
private:
public:
explicit SafeArray(int size = Array::getDefaultSize());
SafeArray(const int *pArr, int size);
//SafeArray(const SafeArray& rhs);
virtual ~SafeArray() { }
//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;
// int size() 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()
{
// automatically Array::~Array() is called
}
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
class Array {
private:
static const int ARRAYSIZE;
protected:
int *pArr_;
int size_;
public:
static int getDefaultSize();
//Array *operator&() const {return this;}
//const Array *operator&() const {return this;}
//Array();
explicit Array(int size = ARRAYSIZE);
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++' 카테고리의 다른 글
Shape (RTTI : Run Time Type Identification) (0) | 2023.09.07 |
---|---|
Shape (Shape, Rectangle, Circle 상속) (0) | 2023.09.07 |
SafeArray (0) | 2023.09.07 |
Queue3 (array 클래스와 has-a 관계) (0) | 2023.09.07 |
Queue2 (생성자 초기화 리스트, static member, explicit) (0) | 2023.09.07 |