מונחה עצמים- תרגול 10 | RTTI and Advanced Exceptions

  • Upload
    ron

  • View
    227

  • Download
    0

Embed Size (px)

Citation preview

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    1/25

    Object Oriented ProgrammingTirgul 10

    Downcasting and RTTI

    Advanced Exceptions

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    2/25

    Downcasting and RTTIdowncastingcasting

    reference.

    :

    Derived* d = new Base();

    ++C.++Cdowncasting

    . :RTTI (Real Time Type Information).++C".

    .

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    3/25

    Downcasting and RTTIRTTI

    .

    . ++C3-RTTI:dynamic_casttypeid

    type_info

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    4/25

    Downcasting and RTTI

    A* p1 = new A();A* p2 = new B();A* p3 = new C();

    /*------------------*/C* p4 = (C*) p3;C* p5 = (C*) p1;B* p6 = (B*) p3;

    A

    B

    C

    // safe// not safe// safe

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    5/25

    dynamic_cast

    B* p6 = dynamic_cast(p1);

    -p1*B.,p1.,0(null pointer.)

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    6/25

    dynamic_castclass Pet {public:

    virtual ~Pet(){}

    };

    class Dog : public Pet{};

    class Cat : public Pet{};

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    7/25

    dynamic_castint main() {

    Pet* b = new Cat();Dog* d1 = dynamic_cast(b);

    Cat* d2 = dynamic_cast(b);

    cout

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    8/25

    typeid & type_info

    typeid2typeid(B) == typeid(*p6);

    typeidreference type_info.type_info==!=

    typeidtrue/false.

    null bad_typeid exception.".

    "name()

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    9/25

    class Grand{

    int hold;

    public:

    Grand(int h=0) : hold(h){}virtual void speak() const {cout

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    10/25

    class Magnificent : public Superb {char ch;

    public:Magnicifent(int h=0, char c=A): Superb(h), ch(c){}void speak() const { coust

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    11/25

    Grand* getObj {

    Grand* p;

    switch(rand() % 3) {case 0: p = new Grand(10);break;

    case 1: p = new Supberb(20);break;case 2: p = new Magnificent(30,M);break;}

    return p;

    }

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    12/25

    #include

    int main {

    srand(time(0));Grand* pg;Superb *ps;

    for (int i = 0; i < 5; ++i) {pg = getObj();cout

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    13/25

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    14/25

    int main {

    Person person;Employee emp;Person* ptr = &emp;

    cout

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    15/25

    exception

    what()

    -

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    16/25

    class exception {public:

    exception () throw();exception (const exception&) throw();

    exception& operator= (const exception&) throw();virtual ~exception() throw();virtual const char* what() const throw();

    }

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    17/25

    #include #include

    using namespace std;

    class Polymorphic {

    virtual void Member(){}

    };

    int main () {try{

    Polymorphic * pb = 0;typeid(*pb); // throws a bad_typeid exception

    } catch (exception& e) {cerr

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    18/25

    exception

    bad_alloc

    bad_castbad_exception

    bad_typeid

    runtime_error

    range_error overflow_error underflow_error

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    19/25

    bad_alloc"new.

    bad_cast"dynamic_cast.

    bad_exceptioncatch.

    bad_typeid"typeid.

    ios_base::failureiostream.

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    20/25

    runtime_error.

    .

    :

    range_error

    overflow_erroroverflow.

    underflow_errorunderflow .

    ++C.

    std::exception

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    21/25

    #include #include

    using namespace std;

    int main () {try

    { int* myarray = new int[1000];}catch (bad_alloc& e){

    cout

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    22/25

    -Fraction.

    ,

    0...

    //Fraction.cpp

    class DivideByZeroException : public runtime_error {public:

    DivideByZeroException(): runtime_error( "attempted to divide by zero" ) {}

    };

    const Fraction operator / (const Fraction &f1, const Fraction &f2) {if(f2.m_iNom == 0)

    throw(DivideByZeroException());int nn = f1.m_iNom * f2.m_iDenom;int dd = f1.m_iDenom * f2.m_iNom;return Fraction (nn, dd);

    }

    istream& operator >> (istream &is, Fraction &frac) {char divSign;is >> frac.m_iNom >> divSign >> frac.m_iDenom;if(frac.m_iDenom == 0)

    throw(DivideByZeroException());frac.reduce ();return is;

    }

    int main(){

    Fraction f1;bool fracOK = false;while (!fracOK){

    try{cin>>f1;fracOK=true;

    } catch (runtime_error& err) {cout

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    23/25

    -...

    -Constructor!

    "

    //Fraction.cpp

    class DivideByZeroException : public runtime_error {public:

    DivideByZeroException(): runtime_error( "attempted to divide by zero" ) {}

    };

    Fraction::Fraction (int nn, int dd) : m_iNom (nn), m_iDenom (dd) {if(m_iDenom == 0)

    throw(DivideByZeroException());reduce ();

    }

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    24/25

    new#include

    #include #include using namespace std;

    void f(){char* pc = newchar[999999999999];//...

    }

    int main(){try{

    f();}catch (bad_alloc& ex){

    cerr

  • 7/30/2019 RTTI and Advanced Exceptions | - 10

    25/25

    Ctor \ new?

    2 ...

    bad_allocnew

    runtime_error

    catch

    )!(