#异常处理#c++
定义一个基类person,包含数据成员姓名、性别、年龄。基类派生出teacher类,它包含数据成员职称,成员函数check),该成员函数可根据职称检测年龄是否正确,检测标准是,教授的年龄大于30岁,副教授的年龄大于26岁,讲师的年龄大于23岁,助教的年龄大于20岁。
运行结果:
代码一:使用try catch语句:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//定义一个基类person,包含数据成员姓名、性别、年龄。
//基类派生出teacher类,它包含数据成员职称,成员函数check),
//该成员函数可根据职称检测年龄是否正确,检测标准是,教授的年龄大于30岁,副教授的年龄大于26岁,讲师的年龄大于23岁,助教的年龄大于20岁。
class person
{
protected:
string name;
string sex; //性别
int age; //年龄
public:
person() { name = ""; sex = ""; age = 0; }
person(string _n, string _s, int _age) :name(_n), sex(_s), age(_age) {}
int getAge() { return age; }
};
class teacher :public person
{
private:
string position; //职位
public:
teacher() {}
teacher(string _n, string _s, int _age, string _p) :person(_n, _s, _age) { position = _p; }
void check()
{
try {
if (position.compare("教授") == 0 && age > 30)
{
cout << name << ":" << position << "," << age << ">30,合法" << endl;
return;
}
else
throw(1);
if (position.compare("副教授") == 0 && age > 26)
{
cout << name << ":" << position << "," << age << ">26,合法" << endl;
return;
}
else
throw(2);
if (position.compare("讲师") == 0 && age > 23)
{
cout << name << ":" << position << "," << age << ">23,合法" << endl;
return;
}
else
throw(3);
if (position.compare("助教") == 0 && age > 20)
{
cout << name << ":" << position << "," << age << ">20,合法" << endl;
return;
}
else
throw(4);
throw(5);
}
catch (int n)
{
switch (n)
{
case 1:
cout << name << ":" << position << "," << age << "<=30,不合法" << endl;
break;
case 2:
cout << name << ":" << position << "," << age << "<=26,不合法" << endl;
break;
case 3:
cout << name << ":" << position << "," << age << "<=23,不合法" << endl;
break;
case 4:
cout << name << ":" << position << "," << age << "<=20,不合法" << endl;
break;
default:
break;
}
}
}
};
int main()
{
teacher t1("张三", "男", 33, "教授");
t1.check();
teacher t2("李四", "男", 20, "副教授");
t2.check();
return 0;
}
代码二:不适用try catch:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//定义一个基类person,包含数据成员姓名、性别、年龄。
//基类派生出teacher类,它包含数据成员职称,成员函数check),
//该成员函数可根据职称检测年龄是否正确,检测标准是,教授的年龄大于30岁,副教授的年龄大于26岁,讲师的年龄大于23岁,助教的年龄大于20岁。
class person
{
protected:
string name;
string sex; //性别
int age; //年龄
public:
person() { name = ""; sex = ""; age = 0; }
person(string _n, string _s, int _age) :name(_n), sex(_s), age(_age) {}
int getAge() { return age; }
};
class teacher :public person
{
private:
string position; //职位
public:
teacher() {}
teacher(string _n, string _s, int _age, string _p) :person(_n, _s, _age) { position = _p; }
int check()
{
if (position.compare("教授") == 0 && age > 30)
return 1;
if (position.compare("副教授") == 0 && age > 26)
return 1;
if (position.compare("讲师") == 0 && age > 23)
return 1;
if (position.compare("助教") == 0 && age > 20)
return 1;
return 0;
}
};
int main()
{
teacher t1("张三", "男", 33, "教授");
if (t1.check())
cout << "张三:教授,年龄"<< t1.getAge() << "大于30岁," << "合法" << endl;
else
cout << "张三:教授,年龄" << t1.getAge() <<"不大于30岁," << "不合法" << endl;
teacher t2("李四", "男", 20, "副教授");
if (t2.check())
cout << "李四:副教授,年龄" << t2.getAge() <<"大于26岁," << "合法" << endl;
else
cout << "李四:副教授,年龄" << t2.getAge() <<"不大于26岁," << "不合法" << endl;
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!