Q1

What is the difference between a try block and a catch block?

Q2

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:

  • The value of lowerLimit is 50, and the value of divisor is 10?
  • The value of lowerLimit is 50, and the value of divisor is 0?
  • The value of lowerLimit is 150, and the value of divisor is 10?
  • The value of lowerLimit is 150, and the value of divisor is 0?

Q3

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!"

Q4

If you define your own exception class, what typically is included in that class?

Q5

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"

Q6

Consider the definition of the class dateType given in Chapter 12.

  • Write the statement that includes a friend function named before in the class dateType that takes as parameters two objects of type dateType and returns true if the date represented by the first object comes before the date represented by the second object; otherwise the function returns false.
  • Write the definition of the function you defined in part a.

Q7

Consider the following declaration:

class strange { . . . };
  • Write a statement that shows the declaration in the class strange to overload the operator >>.
  • Write a statement that shows the declaration in the class strange to overload the operator =.
  • Write a statement that shows the declaration in the class strange to overload the binary operator + as a member function.
  • Write a statement that shows the declaration in the class strange to overload the operator == as a member function.
  • Write a statement that shows the declaration in the class strange to overload the post-increment operator ++ as a member function.

Q8

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?

Q9

What type of value should be returned by a function that overloads a relational operator?

Q10

How many parameters are required to overload the post-increment operator for a class as a friend function?

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.