C++ Exercise #2 a. In a class, why do you include the function that overloads th
ID: 663956 • Letter: C
Question
C++
Exercise #2
a. In a class, why do you include the function that overloads the stream insertion operator, <<, as a friend function?
b. In a class, why do you include the function that overloads the stream extraction operator, >>, as a friend function?
Exercise #3
If you define your own exception class, what is typically included in that class?
Exercise #4
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"
Exercise #5
Name three exception-handling techniques.
Explanation / Answer
Exercise 2:
In a class, why do you include the function that overloads the stream insertion operator, <<, as a friend function?
=> << is output stream operator, we include the function that overloads the stream insertion operator, <<, as a friend function becuase there is no access to the stream object, these can not be member operator that have to be external to a class. Therefore either they must be friends of the class or have access to a public method that wll do streaming for us.
In a class, why do you include the function that overloads the stream insertion operator, <<, as a friend function?
=> >> is input stream operator, we include the function that overloads the stream insertion operator, >>, as a friend function becuase there is no access to the stream object, these can not be member operator that have to be external to a class. Therefore either they must be friends of the class or have access to a public method that wll do streaming for us.
Exercise 3:
1) Constructor; few methods and class memebers; most important try catch on each method for handling exception.
Exercise 4:
What output will be produced if the exception is thrown with the default constructor?
=> Output will be
Immediate attention required!
myException thrown!
what output will be produced if the exception is thrown with the constructor with parameters with the following actual parameter?
"May Day, May Day"
=> output will be
Attention required!
May Day, May Day
Exercise 5:
1) Try and Catch
2) Throws (along with function) like int main() throws IOException {}
3) extends like int main() extends Exception{}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.