Advertisement
Alhiris

Untitled

Jun 8th, 2020
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct B {
  4.     int i;
  5.  
  6. public:
  7.     B() { i = 1; }
  8.     virtual int get_i() { return i; }
  9. } a;
  10. class D : virtual public B {
  11.     int j;
  12.  
  13. public:
  14.     D() { j = 2; }
  15.     int get_i() { return B::get_i() + j; }
  16. };
  17. class D2 : virtual public B {
  18.     int j2;
  19.  
  20. public:
  21.     D2() { j2 = 3; }
  22.     int get_i() { return B::get_i() + j2; }
  23. };
  24. class MM : public D2, public D {
  25.     int x;
  26.  
  27. public:
  28.     MM() { x = D::get_i() + D2::get_i(); }
  29.     int get_i() { return x; }
  30. };
  31. {
  32.     MM b;
  33. }
  34. int main()
  35. {
  36.     B* o = new MM();
  37.     cout << o->get_i() << "\n";
  38.     MM* p = dynamic_cast<MM*>(o);
  39.     if (p)
  40.         cout << p->get_i() << "\n";
  41.     D* p2 = dynamic_cast<D*>(o);
  42.     if (p2)
  43.         cout << p2->get_i() << "\n";
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement