#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
class person {
public :
virtual void showexecpt() = 0;
char * name ;
};
class teacher : public person
{
public :
int earn(){ cout << " earn money " << endl;}
};
class shouketea : public teacher
{
public :
int teaching() { return (rand()%2 +1); }
void showexecpt() { cout << " shouketea is execpted " << endl;}
};
class shishitea : public teacher
{
public:
int manager(){return (rand()%2 +1);}
void showexecpt() { cout << " shishitea is execpted " << endl;}
};
class Stu : public person{
public :
int learn(){return (rand()%2 +1);}
void showexecpt() { cout << " Stu is execpted " << endl;}
};
class shouke {
public :
shouke():pshoutea(new shouketea),pshitea(new shishitea),pstu(new Stu){}
void shouketarget (){
try{
if (1 == pshoutea->teaching() )
throw *pshoutea;
else if (1 == pshitea->manager() )
{
throw *pshitea;
}
else if (1 == pstu->learn())
{
throw *pstu;
}
}
catch(...)
{
throw;
}
}
private :
shouketea * pshoutea;
shishitea * pshitea;
Stu * pstu;
};
int main()
{
srand(time(NULL));
try {
shouke a;
a.shouketarget();
}
catch( person &obj){
obj.showexecpt();
}
return 0;
}
虚函数与异常处理
最新推荐文章于 2022-01-13 19:01:50 发布