Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <exception>
- using namespace std;
- namespace A7R9PQ{
- class Rectangle{
- private:
- double a,b;
- public:
- class myExc: public exception{
- private:
- string error_string;
- public:
- myExc(double value)
- {
- stringstream sst;
- sst<<"The number "<<value<<" is not posivite.";
- error_string=sst.str();
- }
- virtual const char* what() const throw()
- {
- return error_string.c_str();
- }
- ~myExc() throw(){}
- };
- Rectangle(double n1, double n2){ setA(n1); setB(n2);}
- void setA(double a){
- try{
- if(a<0)
- throw myExc(a);
- this->a=a;
- }
- catch(exception& e){
- cout<<e.what()<<endl;
- }
- }
- void setB(double b){
- try{
- if(b<0)
- throw myExc(b);
- this->b=b;
- }
- catch(exception& e){
- cout<<e.what()<<endl;
- }
- }
- double getA()const{ return a;}
- double getB()const{ return b;}
- };
- }
- using namespace A7R9PQ;
- int main(){
- Rectangle t1=Rectangle(3.5,4.9);
- Rectangle t2=Rectangle(1.4,9.9);
- Rectangle t3=Rectangle(2.2,7.35);
- try{
- t1.setA(-2);
- t2.setA(4.3);
- t3.setA(8.8);
- }
- catch (...)
- {
- cout<<"error\n";
- }
- cout<<"1. tlap: a: "<<t1.getA()<<", b: "<<t1.getB()<<endl;
- cout<<"2. tlap: a: "<<t2.getA()<<", b: "<<t2.getB()<<endl;
- cout<<"3. tlap: a: "<<t3.getA()<<", b: "<<t3.getB()<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement