C++程序设计 (第三版) 谭浩强 习题14.2
习题14.2 将例14.3程序改为下面的程序,请分析执行过程,写出运行结果。并指出由于异常处理而调用了哪些析构函数。
IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。
代码块:
#include <iostream>
#include <string>
using namespace std;class Student{
public:Student(int n, string nam){cout<<"constructor - "<<n<<endl;num = n;name = nam;}~Student(){cout<<"destructor - "<<num<<endl;}void get_data();private:int num;string name;
};void Student::get_data(){if(num == 0) throw num;else cout<<num<<" "<<name<<endl;cout<<"in get_data()"<<endl;
}void fun(){Student stud1(1101, "Tan");stud1.get_data();try{Student stud2(0, "Li");stud2.get_data();}catch(int n){cout<<"num = "<<n<<", error!"<<endl;}
}int main(){cout<<"main begin"<<endl;cout<<"call fun()"<<endl;fun();cout<<"main end"<<endl;system("pause");return 0;
}