%--------------------------------------------------------------------------------------------------------------------
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("pause");
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("cls");
cout<<"Televisions : \n\n";
for (int i = 0 ; i < 3; i++)
cout<<televisions[i];
system("pause");
return 0;
}
%--------------------------------------------------------------------------------------------------------------------
Comments
Leave a comment