fork(2) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Top {
  6. protected:
  7. int x;
  8. public:
  9. Top(int n) { x = n; }
  10. virtual ~Top() {}
  11. friend ostream& operator<<(ostream& os, const Top& t) {
  12. return os << t.x;
  13. }
  14. };
  15. class Left : virtual public Top {
  16. protected:
  17. int y;
  18. public:
  19. Left(int m, int n) : Top(m) { y = n; }
  20. };
  21. class Right : virtual public Top {
  22. protected:
  23. int z;
  24. public:
  25. Right(int m, int n) : Top(m) { z = n; }
  26. };
  27. class Bottom : public Left, public Right {
  28. int w;
  29. public:
  30. Bottom(int i, int j, int k, int m): Top(i), Left(0, j), Right(0, k) { w = m; }
  31. friend ostream& operator<<(ostream& os, const Bottom& b) {
  32. return os << b.x << ',' << b.y << ',' << b.z<< ',' << b.w;
  33. }
  34. };
  35. int main() {
  36. Bottom b(1, 2, 3, 4);
  37. cout << sizeof b << endl;
  38. cout << b << endl;
  39. cout << static_cast<void*>(&b) << endl;
  40. Top* p = static_cast<Top*>(&b);
  41. cout << *p << endl;
  42. cout << p << endl;
  43. cout << static_cast<void*>(p) << endl;
  44. cout << dynamic_cast<void*>(p) << endl;
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
28
1,2,3,4
0xbfcce604
1
0xbfcce618
0xbfcce618
0xbfcce604