Answer to Question #50780 in C++ for zexy

Question #50780
A. Create a class named TelevisionException that inherits from runtime_error. The class
constructor should accept a string argument that is passed to the parent as the what()message.
B. Create a class named Television that has data members to hold the model number of a
television, the screen size in inches, and the price. Member functions include overloaded
insertion and extraction operators. If more than four digits are entered for the model
number, if the screen size is smaller than 12 or greater than 70, or if the price is negative or
C. Write a main()function that instantiates a Television object, allows the user to enter
data, and displays the data members. If an exception is caught, replace all the data member
values with zero values. Save the file as Television.cpp.
D. Create a new application that instantiates an array of five television objects and allows the
user to enter data for them. If an exception is caught, make the user reenter the data for
that Television. Save the file as Television2.cpp.
1
Expert's answer
2015-02-23T02:01:44-0500
%--------------------------------------------------------------------------------------------------------------------
Task A
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <string>
using namespace std;
class TelevisionException : public runtime_error{
public:
explicit TelevisionException (const string& msg="") : runtime_error(msg){
};
};
class Television{
private:
int modelNumber;
float screenSize;
float price;
public:
friend istream& operator>>(istream& stream, Television &obj;){
cout<<"Enter the model number (must be greater than 0 and less 10000) : ";
stream>>obj.modelNumber;
if (obj.modelNumber < 1 || obj.modelNumber > 9999) throw TelevisionException("Error!!Model is out of range!!");
cout<<"Enter the screen size (can not be smaller than 12 or greater than 70) : ";
stream>>obj.screenSize;
if (obj.screenSize < 12.00 || obj.screenSize > 70.00) throw TelevisionException("Error!!Screen size is out of range!!");

cout<<"Enter the price (can not be negative) : ";
stream>>obj.price;
if (obj.price< 0) throw TelevisionException("Error!!Price can not be negative!!");
}

friend ostream& operator<<(ostream& stream, const Television& obj){

stream <<"----------------------------------\n"<<endl;
stream <<"Model number : "<<obj.modelNumber<<endl<<endl;
stream <<"Screen size : "<<obj.screenSize<<" inch"<<endl<<endl;
stream <<"Price : "<<obj.screenSize<<" $"<<endl<<endl;
stream <<"----------------------------------\n"<<endl;

return stream;
}
void SetToDefault() {
modelNumber = 0;
screenSize = 0;
price = 0;
}
};
int main(){

Television television;

try{
cin>>television;
}

catch (TelevisionException& exp){
cout<<"Was caught exception!!"<<endl;
cout<<exp.what()<<endl;
television.SetToDefault();
}

cout<<television;
system&#40;"pause"&#41;;
return 0;
}
%--------------------------------------------------------------------------------------------------------------------
Task B
%--------------------------------------------------------------------------------------------------------------------
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <string>
using namespace std;
class TelevisionException : public runtime_error{
public:
explicit TelevisionException (const string& msg="") : runtime_error(msg){
};
};
class Television{
private:
int modelNumber;
float screenSize;
float price;
public:
friend istream& operator>>(istream& stream, Television &obj;){
cout<<"Enter the model number (must be greater than 0 and less 10000) : ";
stream>>obj.modelNumber;
if (obj.modelNumber < 1 || obj.modelNumber > 9999) throw TelevisionException("Error!!Model is out of range!!");
cout<<"Enter the screen size (can not be smaller than 12 or greater than 70) : ";
stream>>obj.screenSize;
if (obj.screenSize < 12.00 || obj.screenSize > 70.00) throw TelevisionException("Error!!Screen size is out of range!!");

cout<<"Enter the price (can not be negative) : ";
stream>>obj.price;
if (obj.price< 0) throw TelevisionException("Error!!Price can not be negative!!");
}

friend ostream& operator<<(ostream& stream, const Television& obj){

stream <<"----------------------------------\n"<<endl;
stream <<"Model number : "<<obj.modelNumber<<endl<<endl;
stream <<"Screen size : "<<obj.screenSize<<" inch"<<endl<<endl;
stream <<"Price : "<<obj.screenSize<<" $"<<endl<<endl;
stream <<"----------------------------------\n"<<endl;

return stream;
}
void SetToDefault() {
modelNumber = 0;
screenSize = 0;
price = 0;
}
};
int main(){

Television televisions[3];

for (int i = 0 ;i < 3; i++)
{
try{
cin>>televisions[i];
}
catch(TelevisionException& exp)
{
cout<<"Was caught exception!!"<<endl;
cout<<exp.what()<<endl;
--i;
}
}
//cleer screen
system&#40;"cls"&#41;;
cout<<"Televisions : \n\n";
for (int i = 0 ; i < 3; i++)
cout<<televisions[i];
system&#40;"pause"&#41;;
return 0;
}


%--------------------------------------------------------------------------------------------------------------------

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS