Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <exception>
  4.  
  5. using namespace std;
  6.  
  7. namespace A7R9PQ{
  8. class Rectangle{
  9. private:
  10. double a,b;
  11. public:
  12. class myExc: public exception{
  13. private:
  14. string error_string;
  15. public:
  16. myExc(double value)
  17. {
  18. stringstream sst;
  19. sst<<"The number "<<value<<" is not posivite.";
  20. error_string=sst.str();
  21. }
  22. virtual const char* what() const throw()
  23. {
  24. return error_string.c_str();
  25. }
  26. ~myExc() throw(){}
  27. };
  28.  
  29.  
  30. Rectangle(double n1, double n2){ setA(n1); setB(n2);}
  31. void setA(double a){
  32. try{
  33. if(a<0)
  34. throw myExc(a);
  35. this->a=a;
  36. }
  37. catch(exception& e){
  38. cout<<e.what()<<endl;
  39. }
  40. }
  41. void setB(double b){
  42. try{
  43. if(b<0)
  44. throw myExc(b);
  45. this->b=b;
  46. }
  47. catch(exception& e){
  48. cout<<e.what()<<endl;
  49. }
  50. }
  51. double getA()const{ return a;}
  52. double getB()const{ return b;}
  53. };
  54. }
  55.  
  56. using namespace A7R9PQ;
  57. int main(){
  58.  
  59. Rectangle t1=Rectangle(3.5,4.9);
  60. Rectangle t2=Rectangle(1.4,9.9);
  61. Rectangle t3=Rectangle(2.2,7.35);
  62. try{
  63. t1.setA(-2);
  64. t2.setA(4.3);
  65. t3.setA(8.8);
  66. }
  67. catch (...)
  68. {
  69. cout<<"error\n";
  70. }
  71. cout<<"1. tlap: a: "<<t1.getA()<<", b: "<<t1.getB()<<endl;
  72. cout<<"2. tlap: a: "<<t2.getA()<<", b: "<<t2.getB()<<endl;
  73. cout<<"3. tlap: a: "<<t3.getA()<<", b: "<<t3.getB()<<endl;
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement