C++基础知识 - 继承与异常

本文探讨了C++中的继承与异常处理。通过一个案例,展示了如何设计一个数组类Vector,重载[]操作,并在不同边界条件下抛出自定义异常,包括errNegativeException、errZeroException、errTooBigException和errTooSmallException。这些异常类都继承自一个名为errSizeException的基类,该基类具有参数化的构造函数和一个虚拟方法printError()用于输出错误信息。
摘要由CSDN通过智能技术生成

继承与异常

  • 异常也是类,我们可以创建自己的异常类,在异常中可以使用(虚函数,派生,引用传递和数据成员等)

  • 案例:设计一个数组类容器 Vector,重载[]操作,数组初始化时,对数组的个数进行有效检查
    1)index<0 抛出异常errNegativeException
    2)index = 0 抛出异常 errZeroException
    3)index>1000抛出异常errTooBigException
    4)index<10 抛出异常errTooSmallException
    5)errSizeException类是以上类的父类,实现有参数构造、并定义virtual void printError()输出错误。

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

class errSizeException {
public:
	errSizeException(int size) {	
		m_size = size;
	}
	virtual void printError() {	//输出Vector数组的大小
		cout << "size: "<< m_size << endl;
	}
protected:
	int m_size;
};

class errNegativeException : public errSizeException {
public:
	errNegativeException(int size) : errSizeException(size) {}
	void printError() {
		cout << "size: " << m_size << endl;
	}
};

class errZeroException : public errSizeException {
public:
	errZeroException(int size) : errSizeException(size) {}
	void printError() {
		cout << "size: " << m_size << endl;
	}
};

class errTooBigException : public errSizeException {
public:
	errTooBigException(int size) : errSizeException(size) {}
	void printError() {
		cout << "size: " << m_size << endl;
	}
};

class errTooSmallException : public errSizeException {
public:
	errTooSmallException(int size) : errSizeException(size) {}
	void printError() {
		cout << "size: " << m_size << endl;
	}
};


class Vector {
public:
	Vector(int len = 64);	
	~Vector();
	int getLen() const;
	int& operator[](int index);

private:
	int *m_base;
	int m_len;	
};
/*
1)index<0    抛出异常 errNegativeException
2)index = 0  抛出异常 errZeroException
3)index>1000 抛出异常 errTooBigException
4)index<10   抛出异常 errTooSmallException
5)errSizeException类是以上类的父类,实现有参数构造、并定义virtual void printError()
*/
Vector::Vector(int len) {

	//抛出普通版本
	if (len < 0) {
		throw errNegativeException(len);
	} else if (len == 0) {
		throw errZeroException(len);
	} else if (len >1000) {
		throw errTooBigException(len);
	} else if (len < 10) {
		throw errTooSmallException(len);
	}

	
	/*
	//抛出动态内存版本
	if (len < 0) {
		throw new errNegativeException(len);
	} else if (len == 0) {
		throw new errZeroException(len);
	} else if (len >1000) {
		throw new errTooBigException(len);
	} else if (len < 10) {
		throw new errTooSmallException(len);
	}
	*/
	m_len = len;
	m_base = new int[len];
}

Vector::~Vector() {
	if (m_base) {
		delete m_base;
		m_base = NULL;
		m_len = 0;
	}
}

int Vector::getLen() const {
	return m_len;
}

int& Vector::operator[](int index) {
	return m_base[index];
}
	
void main(void) {
	try {
		Vector v(-10);
		for (int i = 0; i < v.getLen(); i++) {
			v[i] = i + 1;
			cout << v[i] << endl;
		}
	} catch (errSizeException *error) {	//动态内存版
		error->printError();
		delete error;
	} catch (errSizeException &error) {	//引用版
		error->printError();
		delete error;
	}
	
	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值