- 注册
- 2003-04-18
- 消息
- 9,432
- 荣誉分数
- 84
- 声望点数
- 158
代码:
class D1 : public Base {
public:
D1(int b, int d1) : Base(b);
~D1();
我想问,如果D1 constructor 被call的话,一个Base object也会被created对不对?
如果D1 destructor被call呢?Base 的destructor会不会被call?假设~D1里面没有“~Base()”这么一行。
代码:
#include <iostream>
using namespace std;
class DivideByZero{
public: void print();
};
class Negative{
public: void print();
};
void DivideByZero:: print(){
cout << “Divide by Zero” << endl;
}
void Negative:: print(){
cout << “Negative” << endl;
}
void bottom(int i){
if (i == 0) throw DivideByZero();
cout << “100/i == “ << 100/i << endl;
}
void middle(int i){
try {
bottom (i);
if (i < 0) throw Negative();
}
catch (DivideByZero &e){
cout << “In middle: “;
e.print();
}
}
void top(int i){
middle(i);
try{
middle(10);
}
catch (...){
cout << “Error in top” << endl;
}
}
int main(){
int i; cin >> i;
try{
top(i);
}
catch (Negative){
cout << “Negative in main” << endl;
}
catch (DivideByZero){
cout << “Divide by zero in main” << endl;
}
cout << “Done” << endl; return 0;
}
我想问,如果i=-10的话,output是什么?
如果i = 0,output是什么?