What is the difference between a try block and a catch block?
Consider the following C++ code:
double balance;
try
{
cout << "Enter the balance: ";
cin >> balance;
cout << endl;
if (balance < 1000.00)
throw balance;
cout << "Leaving the try block." << endl;
}
catch (double x)
{
cout << "Current balance: " << x << endl
<< "Balance must be greater than 1000.00" << endl;
}
cout << "Exiting the try block." << endl;
}
catch (int x)
{
cout << "Exception: " << x << endl;
result = 120;
}
catch (string str)
{
cout << "Exception: " << str << endl;
}
cout << "After the catch block" << endl;
What is the output if:
Define an exception class called tornadoException. The class should have two constructors, including the default constructor. If the exception is thrown with the default constructor, the method what should return "Tornado: Take cover immediately!". The other constructor has a single parameter, say, m, of the int type. If the exception is thrown with this constructor, the method what should return "Tornado: m miles away; and approaching!"
If you define your own exception class, what typically is included in that class?
Suppose the exception class myException is defined as follows:
class myException
{
public:
myException()
{
message = "myException thrown!";
cout << "Immediate attention required!"
<< endl;
}
myException(string msg)
{
message = msg;
cout << "Attention required!" << endl;
}
string what()
{
return message;
}
private:
string message;
}
Suppose that in a user program, the catch block has the following form:
catch (myException mE)
{
cout << mE.what() << endl;
}
What output will be produced if the exception is thrown with the default constructor? Also, what output will be produced if the exception is thrown with the constructor with parameters with the following actual parameter? "May Day, May Day"
Consider the definition of the class dateType given in Chapter 12.
Consider the following declaration:
class strange { . . . };
In a class, why do you include the function that overloads the stream insertion operator, <<, as a friend function?
In a class, why do you include the function that overloads the stream extraction operator, >>, as a friend function?
What type of value should be returned by a function that overloads a relational operator?
How many parameters are required to overload the post-increment operator for a class as a friend function?