-
What is the output of this program?
#include <iostream>
using namespace std;
class BaseClass
{
protected:
int num;
public:
BaseClass()
{
num = 25;
}
BaseClass(int k)
{
num = k;
}
virtual ~BaseClass()
{
if (num < 0) throw num;
}
virtual int getA()
{
if (num < 0)
{
throw num;
}
}
};
int main()
{
try
{
BaseClass obj(-23);
cout << endl << obj.getA();
}
catch (int)
{
cout << endl << "Illegal initialization";
}
}
-
- Runtime Error
- Compilation Error
- Illegal initialization
- Garbage value
- None of these
Correct Option: B
Compilation Error
main.cpp: In destructor ‘virtual BaseClass::~BaseClass()’:
main.cpp:18:33: warning: throw will always call terminate() [-Wterminate]
if (num < 0) throw num;
^~~
main.cpp:18:33: note: in C++11 destructors default to noexcept
main.cpp: At global scope:
main.cpp:39:5: fatal error: error writing to /tmp/cci28TfS.s: No space left on device
}
^
compilation terminated.