Guest User

Untitled

a guest
Jun 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Base
  6. {
  7. public:
  8.     virtual ~Base() {}
  9.     virtual int foo() = 0;
  10. };
  11.  
  12. class a : public Base
  13. {
  14. public:
  15.     a() {
  16.         i=666;
  17.     }
  18.     virtual int foo() {
  19.         return i;
  20.     }
  21. private:
  22.     int i;
  23. };
  24. class b
  25. {
  26. public:
  27.     b() {
  28.         i=44;
  29.     }
  30.     int bar() {
  31.         return -i;
  32.     }
  33.  
  34.     int i;
  35. };
  36. ostream& operator<<(ostream& cout,a& obj)
  37. {
  38.     cout << "funkcja dla klasy a:" << obj.foo();
  39.     return cout;
  40. }
  41. ostream& operator<<(ostream& cout, b& obj)
  42. {
  43.     cout << "funkcja dla klasy b:" << obj.bar();
  44.     obj.i = 0;
  45.     return cout;
  46. }
  47. union unia {
  48.     a* uA;
  49.     b* uB;
  50. };
  51. int main()
  52. {
  53.     unia u;
  54.     u.uA = new a();
  55.     cout << *u.uA << '\n';
  56.     cout << *u.uB << '\n';
  57.     cout << *u.uA << '\n';
  58.     std::cin.ignore();
  59.     std::cin.sync();
  60. }
Add Comment
Please, Sign In to add comment