Define an exception class called tornadoException. The class should have two con
ID: 3807044 • Letter: D
Question
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!". Write a program to test this class. my biggest problem is getting it to display the distance. (all in c++): heres my code so far:
#include<iostream>
#include<string>
#include <exception>
using namespace std;
class tornadoException
{
public:
tornadoException()
{
message = " Tornado: take cover immediately";
}
tornadoException(int m)
{//this is where im having trouble tried "m" +m+ needs to display miles
message = "tornado is:+m+ miles away ";
}
string what()
{
return message;
}
private:
string message;
};
int main()
{
int miles;
try{
cout<< "Enter miles";
cin >> miles;
cout << endl;
if(miles <= 0){
throw tornadoException();
}
else{
throw tornadoException(miles);
}
}
catch(tornadoException ex)
{
cout << " In TornadoException "<< endl
<< ex.what()<<endl;
}
return 0;
}
Explanation / Answer
// C++ code
#include <iostream>
#include <string>
#include <exception>
using namespace std;
class tornadoException
{
public:
tornadoException()
{
message = " Tornado: take cover immediately";
}
tornadoException(int m)
{ // convert integer to string add to message
message = "tornado is: " + to_string(m) + " miles away ";
}
string what()
{
return message;
}
private:
string message;
};
int main()
{
int miles;
try{
cout<< "Enter miles: ";
cin >> miles;
cout << endl;
if(miles <= 0){
throw tornadoException();
}
else{
throw tornadoException(miles);
}
}
catch(tornadoException ex)
{
cout << "In TornadoException "<< endl << ex.what()<<endl;
}
return 0;
}
/*
output:
Enter miles: -10
In TornadoException
Tornado: take cover immediately
Enter miles: 10
In TornadoException
tornado is: 10 miles away
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.