Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;
  4.  
  5. class Base {
  6. public:
  7. void f() {
  8. std::cout << 2 << " ";
  9. }
  10. virtual void g() {
  11. std::cout << 7 << " ";
  12. }
  13. };
  14.  
  15.  
  16. class Derived:public Base {
  17. public:
  18. void f() {
  19. std::cout << 1 << " ";
  20. }
  21. void g() {
  22. std::cout << 3 << " ";
  23. }
  24. virtual void h() {
  25. std::cout << 4 << " ";
  26. }
  27. };
  28.  
  29.  
  30. class Young:public Derived {
  31. public:
  32. void h() {
  33. std::cout << 9 << " ";
  34. }
  35. };
  36.  
  37.  
  38.  
  39. int main() {
  40. Base *a;
  41. Derived b;
  42. Base *c = new Base;
  43. Base *d = new Derived;
  44. Derived *e = new Derived;
  45. Base *u = new Young;
  46.  
  47. cout << (static_cast<Derived*>(c)==nullptr) << " ";
  48. cout << (dynamic_cast<Derived*>(c)==nullptr) << " ";
  49. cout << (static_cast<Derived*>(d)==nullptr) << " ";
  50. cout << (dynamic_cast<Derived*>(d)==nullptr) << " ";
  51. cout << (static_cast<Derived*>(u)==nullptr) << " ";
  52. cout << (dynamic_cast<Derived*>(u)==nullptr) << " " << endl;
  53. cout << (typeid(*u)==typeid(Base)) << " ";
  54. cout << (typeid(*u)==typeid(Derived)) << " ";
  55. cout << (typeid(*u)==typeid(Young)) << " " << endl;
  56. return 0;
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement