At which time does the static_cast can be applied?
What is meant by type_info?
What is the output of this program?
1. #include2. using namespace std; 3. struct A 4. { 5. virtual void f() 6. { 7. cout << "Class A" << endl; 8. } 9. }; 10. struct B : A 11. { 12. virtual void f() 13. { 14. cout << "Class B" << endl; 15. } 16. }; 17. struct C : A 18. { 19. virtual void f() 20. { 21. cout << "Class C" << endl; 22. } 23. }; 24. void f(A* arg) 25. { 26. B* bp = dynamic_cast(arg); 27. C* cp = dynamic_cast (arg); 28. if (bp) 29. bp -> f(); 30. else if (cp) 31. cp -> f(); 32. else 33. arg -> f(); 34. }; 35. int main() 36. { 37. A aobj; 38. C cobj; 39. A* ap = &cobj; 40. A* ap2 = &aobj; 41. f(ap); 42. f(ap2); 43. }
What is the output of this program?
1. #include2. #include 3. using namespace std; 4. class A 5. { 6. public: 7. virtual ~A(); 8. }; 9. int main() 10. { 11. A* a = NULL; 12. try 13. { 14. cout << typeid(*a).name() << endl; 15. } 16. catch (bad_typeid) 17. { 18. cout << "Object is NULL" << endl; 19. } 20. }
What is the output of this program?
1. #include2. #include 3. #include 4. using namespace std; 5. class base 6. { 7. virtual void f(){} 8. }; 9. class derived : public base {}; 10. int main () 11. { 12. try 13. { 14. base* a = new base; 15. base* b = new derived; 16. cout << typeid(*a).name() << 't'; 17. cout << typeid(*b).name(); 18. } 19. catch (exception& e) 20. { 21. cout << "Exception: " << e.what() << endl; 22. } 23. return 0; 24. }