What happens when this C++ program is compiled?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func()
{
B b;
throw b;
}
int main()
{
try{
func();
}
catch(A a){
cout<<"Caught A Class\n";
}
catch(B b){
cout<<"Caught B Class\n";
}
}
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func()
{
B b;
throw b;
}
int main()
{
try{
func();
}
catch(A a){
cout<<"Caught A Class\n";
}
catch(B b){
cout<<"Caught B Class\n";
}
}
A. The program compiles successfully without any errors or warnings
B. Compile-time error occurs
C. The program compiles successfully with warnings
D. The program gives both errors and warnings
Answer: Option C
Join The Discussion