class ArrayObj {
public:
~ArrayObj();
ArrayObj(int len);
public:
int& operator[](int index);
int getLen();
class eSize {
public:
eSize(int size) {
m_size = size;
}
virtual void printErr(){
cout << m_size << " 错误-----" << endl;
}
protected :
int m_size;
};
class eNegative:public eSize {
public:
eNegative(int size) :eSize(size) {
}
void printErr() {
cout << m_size << " negative错误-------" << endl;
}
};
class eZero:public eSize{
public:
eZero(int size) :eSize(size) {
}
void printErr() {
cout << m_size << " zero错误-------" << endl;
}
};
class eSmall:public eSize {
public:
eSmall(int size) :eSize(size) {
}
void printErr() {
cout << m_size << " small错误-------" << endl;
}
};
class eBig :public eSize {
public:
eBig(int size) :eSize(size) {
}
void printErr() {
cout << m_size << " big错误-------" << endl;
}
};
private:
int* m_space;
int m_len;
};
ArrayObj::ArrayObj(int len) {
if (len < 0) {
throw eNegative(len);
}
else if (len == 0) {
throw eZero(len);
}
else if (len > 1000) {
throw eBig(len);
}
else if (len < 3) {
throw eSmall(len);
}
m_len = len;
m_space = new int[len];
}
ArrayObj::~ArrayObj() {
if (m_space != NULL) {
delete []m_space;
m_space = NULL;
}
}
int& ArrayObj::operator[](int index) {
return m_space[index];
}
int ArrayObj::getLen() {
return m_len;
}
int main1() {
try {
ArrayObj ab(-5);
for (int i = 0; i < ab.getLen(); i++) {
ab[i] = i + 1;
}
}
catch (ArrayObj::eNegative& e) {
cout << "负数错误-----" << endl;
}
catch (ArrayObj::eSmall& e) {
cout << "太小错误..." << endl;
}
catch (...) {
cout << "其他类型错误...." << endl;
}
return 0;
}
int main() {
try {
ArrayObj ab(2);
for (int i = 0; i < ab.getLen(); i++) {
ab[i] = i + 1;
}
}
catch (ArrayObj::eSize& e) {
e.printErr();
}
catch (...) {
cout << "其他类型错误...." << endl;
}
system("pause");
return 0;
}
05-23
2649
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-25
2814
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-26
542
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交